I have some JSO开发者_如何学PythonN code in a string that I am trying to parse. I havent used JSON much so this is prolly a simple question.
It is like:
$json_code ="
{
"key1":"value",
"key2":"value"
},
{
"key3":"value",
"key4":"value"
}";
Im having problems trying to loop through all of different Objects(? - the sets of curly braces) with php.
Any help is greatly appreciated
Thanks, Bryan
You can't use a JSON literal in PHP like that. Turn it into a string (wrap it in quotes), and then use json_decode()
to access it in an object like manner.
If you'd prefer to access it like an array, set json_decode()
's second argument to TRUE
.
Update
I see you have wrapped it in quotes - you must now escape the inner quotes.
To loop through it, just use foreach()
on the object or array returned from json_decode()
.
To visualise the structure once parsed via json_decode()
, use var_dump()
.
Update
Your problem is, your JSON is not proper - it has 2 objects, but not in array literal syntax. You need to wrap that structure with []
.
See it.
精彩评论