Reputation: 9
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?
Upvotes: 1
Views: 53
Reputation: 26
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.
Upvotes: 0
Reputation: 71
You have to put the string
today
In double qoutes
"today"
Because its a string :)
Upvotes: 2