While answering Get JSON object from URL Difficulties, I noticed one of the JSON names was "$id":
{ "data" : [
{
"$id": "1",
"SearchKey": "Alnwick |Alnwick",
...
This caused the following php code to throw different errors:
$json = ... //json above
$obj = json_decode($json);
echo property_exists($obj->data[0], '$id'); // prints true
echo $obj->data[0]->$id; // PHP Fatal Error: Cannot access empty property ...
echo $obj->data[0]->id; // PHP Notice: Undefined property stdClass::$id ...
echo $obj->data[0]->'$id'; // PHP Parse Error: syntax error, unexpected ''$id'' (T_CONSTANT_ENCAPSED_STRING) ...
Assuming the json is decoded as objects not arrays, how can I access the "$id" field?
Accessing the variable via {'invalid-parameter-name'} works:
echo $obj->data[0]->{'$id'}; // 1
Related
I have an operation like the following code:
$t = response()->json($response);
When I return $t, the following response is displayed in my browser:
{"orderId":183,"redirectUrl":"https:\/\/sandbox.domian.com\/tg\/start\/1518"}
How can I get the redirectUrl from this response?
I tried this following codes but got an error:
return $t->redirectUrl;
return json_decode($t)['redirectUrl'];
Error: Undefined property: Illuminate\Http\JsonResponse::$redirectUrl
You need to use json_decode and set the 2nd parameter to true if you want the result as an array. If you don't set the 2nd parameter, json_decode will likely return an object of type StdClass in which you can access the properties with $obj->property syntax.
http://sandbox.onlinephpfunctions.com/code/a6c62c9dbe7d343124648d18f4cb40e828634350
<?php
$json = '{"orderId":183,"redirectUrl":"https:\/\/sandbox.domian.com\/tg\/start\/1518"}';
// Notice the 2nd parameter - https://www.php.net/manual/en/function.json-decode.php#refsect1-function.json-decode-parameters
echo "Via array: " . json_decode($json, true)['redirectUrl'];
// Or
echo "\nVia object: " . json_decode($json)->redirectUrl;
I'm learning a bit of PHP. It's supposed to print 0, however I get an error:
Notice: Trying to get property of non-object in...
<?php
$json = '[{"assetref":"","qty":0,"raw":0}]';
$obj = json_decode($json);
print $obj->{'qty'}; // Result 0
?>
The brackets on the outside of your JSON string are causing it to be an object inside of an array.
You can access the object by specifying which array member you want with $obj[0]->{'qty'};
OR change your json string so it instantiates into an object directly.
$json = '{"assetref":"","qty":0,"raw":0}';
I'm learning a bit of PHP. It's supposed to print 0, however I get an error:
Notice: Trying to get property of non-object in...
<?php
$json = '[{"assetref":"","qty":0,"raw":0}]';
$obj = json_decode($json);
print $obj->{'qty'}; // Result 0
?>
The brackets on the outside of your JSON string are causing it to be an object inside of an array.
You can access the object by specifying which array member you want with $obj[0]->{'qty'};
OR change your json string so it instantiates into an object directly.
$json = '{"assetref":"","qty":0,"raw":0}';
I'm trying to parse the following Json file:
{
"Itineraries" : [{
"date1" : "20/Jan/2016",
"date2" : "20/Jan/1996",
"Options" : [
{
"Num_ID" : [398],
"Quotedwhen" : today,
"Price" : 330.00
}
]
}
]
}
I'm using the following PHP code:
$json2 = file_get_contents("data.json");
var_dump(json_decode($json2));
$parsed_json2 = json_decode($json2);
$price = $parsed_json2->{'Itineraries'}->{'Options'}->{'Price'};
And I get the following error (Line 35 is the last line of the PHP code above):
Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/php/jsonread.php on line 35
Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/php/jsonread.php on line 35
Do you have any idea of how to solve this problem?
You have to put the string
today
In double qoutes
"today"
Because its a string :)
the reason you are getting that message is because the json_decode() is failing to return an object because your JSON is invalid. You need to put double quotes around today. You are also accessing the data incorrectly.
Here's the correct code to get the price:
echo($parsed_json2->Itineraries[0]->Options[0]->Price);
You have created a lot of arrays here which only have one item in them, are you intending to have multiple itinerary, multiple options objects, and multiple Num_IDs per options object? If not you can get rid of a lot of those square brackets.
I would like to get the value of "Records"
When is try this code:
$json = file_get_contents('URL');
var_dump($json);
This is the result:
string(25289) "{ "ProductsSummary": { "Records": 10, "TotalRecords": 2874, "TotalPages": 288, "CurrentPage": 1 }, "Products": [ { "...
When I try this code
$json = file_get_contents('URL');
$obj = json_decode($json);
echo $obj;
echo $obj->{'ProductsSummary'}->{'Records'};
echo $obj->ProductsSummary->Records;
echo $obj[0]->ProductsSummary->Records;
echo $obj->ProductsSummary[0]->Records;
echo $obj->ProductsSummary[1];
The Output is:
{ "ProductsSummary": { "Records": 10, "TotalRecords": 2879, "TotalPages": 288, "CurrentPage": 1 }, "Products": [ { "Last.... }
Notice: Trying to get property of non-object in ...
Notice: Trying to get property of non-object in ...
Notice: Trying to get property of non-object in ...
Since you do not have valid JSON, I'm pretty sure that json_decode() is just failing. As per the manual:
Returns the value encoded in json in appropriate PHP type. Values
true, false and null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot be decoded or if the
encoded data is deeper than the recursion limit.
You can verify that with e.g. the is_null() function:
$obj = json_decode($json);
if( is_null($obj) ){
// Invalid JSON, don't need to keep on working on it
}else{
// Read data
}
... although the proper way would be to explicitly check for errors with json_last_error(), which should equal JSON_ERROR_NONE unless something went wrong.
If everything works fine, $obj will be an object, thus feeding echo with it will never yield anything useful:
echo $obj;
You might want to use var_dump() instead.
$json is a string. You should decode it first using json_decode($json)