如何访问嵌套JSON的某些部分? [英] How to access certain part of a nested JSON?

查看:108
本文介绍了如何访问嵌套JSON的某些部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过以下代码访问文本"的内容.实际上,我之前曾问过几乎相同的问题,并且它已经完美地解决了. 如何访问JSON的某些部分?但是,何时我在此JSON上使用了相同的方法,但无法正常工作.

I'm trying to access the content of "text" in the following code. Actually I have asked almost the same question before and it was solved perfectly. How to access certain part of a JSON? However, when I use the same method on this JSON, it doesn't work.

$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,我不知道该怎么办.有人可以帮我吗?谢谢.

The output is NULL and I don't know what to do. Can anyone help me with it? Thanks.

推荐答案

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

The tried and tested json_decode(json_encode($test), true); Does the job for me. Cheers!

$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]);

这篇关于如何访问嵌套JSON的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆