Having read all the articles I can find I’m left wondering if the json data is valid.
I’m using php to make this request :
$myjson= file_get_contents('http://slhassall.rightboatexpert.com/api/boats');
var_dump(json_decode($myjson));
and this dumps the below to the page, is it valid json?
object(stdClass)[1]
public 'pagination' =>
object(stdClass)[2]
public 'total' => int 32
public 'num_per_page' => int 25
public 'page' => int 1
public 'results' =>
array (size=25)
0 =>
object(stdClass)[3]
public 'id' => int 54
public 'manufacturer' => string 'Utrecht' (length=7)
public 'condition' => string 'used' (length=4)
public 'model' => string 'Iron Sailing Barge' (length=18)
public 'marketing_status' => string 'For Sale' (length=8)
public 'year' => null
public 'stock_number' => null
public 'location' => string 'Suffolk' (length=7)
public 'description' => string 'Clipper design. Built in Utrecht 1988, 25m in
length, an elegantly converted Dutch sailing barge with fantastic internal and external
social entertainment /display spaces. Suitable for corporate or pri' (length=201)
public 'sale_status' => string 'For Sale' (length=8)
public 'price' => int 149000
public 'currency' => string 'GBP' (length=3)
public 'thumbnail' => string '/api/images/262' (length=15)
public 'boat_image_id' => int 262
1 =>
object(stdClass)[4]
public 'id' => int 51
public 'manufacturer' => string ' Wood' (length=5)
public 'condition' => string 'used' (length=4)
public 'model' => string 'MFV/Guard vessel ' (length=17)
public 'marketing_status' => string 'For Sale' (length=8)
public 'year' => int 1959
public 'stock_number' => null
public 'location' => string 'Great Yarmouth, Norfolk UK' (length=26)
public 'description' => string '' (length=0)
public 'sale_status' => string 'For Sale' (length=8)
public 'price' => int 35000
public 'currency' => string 'GBP' (length=3)
public 'thumbnail' => string '/api/images/219' (length=15)
public 'boat_image_id' => int 219
2 =>
I ask because when i attempt to connect to a node:
$array = (json_decode($myjson));
echo $array->boat->manufacturer;
I get this error:
Notice: Undefined property: stdClass::$boat in C:\wamp\www\json\index.php on line 6
Notice: Trying to get property of non-object in C:\wamp\www\json\index.php on line 6
With line 6 being the echo statement.
I have Googled these error codes but can’t figure it out.
Any help would be appreciated.
Thanks
Your JSON is valid, as invalid JSON would result in returning
NULL:http://php.net/manual/en/function.json-decode.php
Though I recommend you to do a checking after calling
json_decodeso that you know what is going on when you access the object later and found that you are accessing property of non-object (i.e.NULL)You get the error because your object isn’t structured that way – as your
var_dumptold you, there is no propertyboatin the resultant object.From your
var_dump, it means the object has two propertiespaginationandresults. Inresultsthere is an array consisting 25 items. In each item there is an object having propertiesid,manufacturer,conditionand so on.And, indeed your question contains no JSON, php
var_dumpdoes not give you a JSON…I am not sure what do you want to do. I guess you want to print all manufacturers of each boat, so… (untested)