Skip to content
Advertisement

Php json parse slashes and quotes

I’m new in php, I’ve never seen a json file with slashes and quotes before.

{
    "datas": "{"_id":{"testid":[14,49]},"newid":44235,"type":{"_id":3,"name":"umbrella"}}"
}

I tried codes like this but it didn’t work

$url = '/test.json';
 
$json = file_get_contents($url);
 
$arr = json_decode($json);

foreach($arr->datas AS $data){
    echo $data->name;
}

Sorry for my bad English, thank you.

Advertisement

Answer

The value of datas is JSON as well, so you need to decode that string after decoding the overall JSON.

$arr = json_decode($json);
$datas = json_decode($arr->datas);

As the element you are after isn’t in an array and is just the property of an object, you can access it using…

echo $datas->type->name;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement