Parse JSON in string format via PHP

390 Views Asked by At

I have a json file from import.io that returns null when decoded, but shows up as a string when encoded and is all there. How can I "loop" through a json string in PHP?

Json data is very lengthy so I refrained from posting it.

Json: https://codeshare.io/2BD4ma

Code:

<?php

$jsonFile = file_get_contents('feeds/quotes.json');

//decode
$results = json_encode($jsonFile, TRUE);

var_dump($results);


?>
1

There are 1 best solutions below

14
Stuart On

It would be nice to see the code you are using, or have tried....

Either way, you need to use json_decode on the JSON object, which will turn it into a PHP array:

$data = json_decode($yourJsonData);

// print_r it to see:
print_r($data);

// to loop through it, you could do:
foreach ($data as $item)
{
    print_r($item); // used print_r: unsure if this data will contain nested objects/arrays
}