Php

Asked • 06/05/19

How can I parse a JSON file with PHP?

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.

1 Expert Answer

By:

Benjamin S. answered • 11/15/19

Tutor
4.8 (110)

Lead Software Engineer Specializing in PHP

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.