I tried to parse a JSON file using PHP. But I am stuck now.
This is the content of my JSON file:
{
"John": {
"status":"Wait"
},
"Jennifer": {
"status":"Active"
},
"James": {
"status":"Active",
"age":56,
"count":10,
"progress":0.0029857,
"bad":0
}
}
And this is what I have tried so far:
<?php
$string = file_get_contents("/home/michael/test.json");
$json_a = json_decode($string, true);
echo $json_a['John'][status];
echo $json_a['Jennifer'][status];
But because I don't know the names (like 'John'
, 'Jennifer'
) and all available keys and values (like 'age'
, 'count'
) beforehand, I think I need to create some foreach loop.
I would appreciate an example for this.
The quickest way to echo all json values is using loop in loop, the first loop is going to get all the objects and the second one the values...
More standard answer:
And the output is:
Try it:
The most elegant solution:
Remember that the json-file has to be encoded in UTF-8 without BOM. If the file has BOM, then json_decode will return NULL.
Alternatively: