从php中的多级json获取字符串
我试图从json中获取一些字符串,但尚未成功:( 我尝试了许多代码,但仍然没有运气,不能完全理解这一点 json示例是:从php中的多级json获取字符串,php,json,Php,Json,我试图从json中获取一些字符串,但尚未成功:( 我尝试了许多代码,但仍然没有运气,不能完全理解这一点 json示例是: { "nginx_version": "1.7.11.3 Gryphon", "nginx_rtmp_version": "1.1.4", "built": "Mar 19 2015 20:36:41", "pid": "4680", "uptime": "1876", "naccepted": "3", "bw_in"
{
"nginx_version": "1.7.11.3 Gryphon",
"nginx_rtmp_version": "1.1.4",
"built": "Mar 19 2015 20:36:41",
"pid": "4680",
"uptime": "1876",
"naccepted": "3",
"bw_in": "404904",
"bytes_in": "83699680",
"bw_out": "404912",
"bytes_out": "83700478",
"server": {
"application": [{
"name": "msdk",
"live": {
"stream": {
"name": "xkiz531",
"time": "1854519",
"bw_in": "399752",
"bytes_in": "82628640",
"bw_out": "399752",
"bytes_out": "82628588",
"bw_audio": "178480",
"bw_video": "221272",
"client": [{
"id": "2788",
"address": "example.com\/live\/live",
"time": "1854519",
"flashver": "ngx-local-relay",
"dropped": "0",
"avsync": "-14",
"timestamp": "1853566",
"active": []
}, {
"id": "2787",
"address": "197.14.103.17",
"time": "1854987",
"flashver": "FMLE\/3.0 (compatible; FMSc\/1.0)",
"swfurl": "rtmp:\/\/example.com\/msdk",
"dropped": "0",
"avsync": "-14",
"timestamp": "1853566",
"publishing": [],
"active": []
}],
"meta": {
"video": {
"width": "1280",
"height": "720",
"frame_rate": "30",
"codec": "H264",
"profile": "High",
"compat": "0",
"level": "3.1"
},
"audio": {
"codec": "AAC",
"profile": "LC",
"channels": "2",
"sample_rate": "44100"
}
},
"nclients": "2",
"publishing": [],
"active": []
},
"nclients": "2"
}
}, {
"name": "test",
"live": {
"nclients": "0"
}
}]
}
}
我现在的代码是:
$array = json_decode($json,TRUE);
var_dump($array);
foreach($array['items'] as $item) {
echo $item['server']['application']['live']['stream']['name'];
}
我想获取字符串上这些键的值:
bw_音频、bw_视频、宽度、高度、帧速率、编解码器、配置文件、音频编解码器和采样率您在JSON中没有
项
键,应用程序是一个数组,因此您应该执行以下操作:
$array = json_decode($json,TRUE);
echo $array['server']['application'][0]['live']['stream']['name'];
注意:如果可能有多个应用程序,请在数组上迭代foreach
,而不是使用[0]
我在JSON中没有看到项。application
是一个数组。您需要使用['application'][0]
或对其进行迭代。您想要获取键bw_音频
,bw_视频
,还是想要获取这些键的值?键的值…………很抱歉,我将更新我的问题我仍然看不到$array['items]的位置
是。我假设您显示的JSON是$array
@chedlywalid您在JSON中没有项
键。我编辑了我的答案。