如何访问嵌套JSON的某些部分?

时间:2019-06-10 04:24:18

标签: php json

我正在尝试通过以下代码访问“文本”的内容。实际上,我之前曾问过几乎相同的问题,并且已完美解决。 How to access certain part of a JSON?但是,当我在此JSON上使用相同的方法时,它将无法正常工作。

$test = 
'{
    "username":"lon",
    "event":{
        "saved_response":"{\"parts\": [{\"text\": \"Passion for teaching means loving your job. Doing with all your heart. Teachers who are passionate can inspire pupils to love learning. Passionate teachers create an effective learning environment and increase learning potential of\\nstudents.\"}]}"
    },
    "event_source":"server"
}';

$jarray = json_decode($test, true);
$jevent = json_decode($jarray['event']['saved_response'], true);
var_dump($jevent);
echo $jevent['parts'][0]['text'];

输出为NULL,我不知道该怎么办。有人可以帮我吗?谢谢。

1 个答案:

答案 0 :(得分:1)

久经考验的json_decode(json_encode($test), true); 为我做这份工作。干杯!

$test = 
'{
"username":"lon",
"event":{
    "saved_response":"{\"parts\": [{\"text\": \"Passion for teaching means loving your job. Doing with all your heart. Teachers who are passionate can inspire pupils to love learning. Passionate teachers create an effective learning environment and increase learning potential of\\nstudents.\"}]}"
},
"event_source":"server"
}';

$jarray = json_decode(json_encode($test), true);

$jarray = explode('"',$jarray);

print_r($jarray[14]);