我调用一个以json格式返回数据的url。我喜欢从这个输出中挑选出一些变量。输出如下:
{
"graph_property" : [ {
"name" : "calculation_method",
"value" : "Geo Mean"
}, {
"name" : "graph_type",
"value" : "TIME"
} ],
"measurement" : [ {
"id" : "1132282",
"alias" : "example.com"
"bucket_data" : [ {
"name" : "2013-JUN-20 11:23 AM",
"id" : 1,
"perf_data" : {
"value" : "4.878",
"unit" : "seconds"
},
"avail_data" : {
"value" : "100.00",
"unit" : "percent"
},
"data_count" : {
"value" : "1",
"unit" : "#"
}
}, {
"name" : "2013-JUN-20 11:28 AM",
"id" : 2,
"perf_data" : {
"value" : "-",
"unit" : "seconds"
},
"avail_data" : {
"value" : "-",
"unit" : "percent"
},
"data_count" : {
"value" : "-",
"unit" : "#"
}
}, {
"name" : "2013-JUN-20 11:33 AM",
"id" : 3,
"perf_data" : {
"value" : "-",
"unit" : "seconds"
},
"avail_data" : {
"value" : "-",
"unit" : "percent"
},
"data_count" : {
"value" : "-",
"unit" : "#"
}
} ],
"graph_option" : [ {
"name" : "perfwarning",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "perfcritical",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "availwarning",
"value" : "-",
"unit" : "percent"
}, {
"name" : "availcritical",
"value" : "-",
"unit" : "percent"
}, {
"name" : "bucketsize",
"value" : "300",
"unit" : "seconds"
}, {
"name" : "rows",
"value" : "3",
"unit" : "#"
}, {
"name" : "pagecomponent",
"value" : "User Time",
"unit" : "seconds"
}, {
"name" : "avg_perf",
"value" : "4.878",
"unit" : "seconds"
}, {
"name" : "avg_avail",
"value" : "100.00",
"unit" : "percent"
}, {
"name" : "total_datapoint_count",
"value" : "1",
"unit" : "#"
}, {
} ]
} ],
"link" : {
"type" : "application/json",
"href" : "http://api.keynote.com/",
"rel" : "slotmetadata"
}
}
我感兴趣的价值是这些:
"
name" : "2013-JUN-20 11:23 AM",
"value" : "4.878",
"name" : "2013-JUN-20 11:28 AM",
"value" : "-",
"name" : "2013-JUN-20 11:33 AM",
"value" : "-",
我这样做的原因是,有时,Web服务调用会返回emtpy值。从这个输出中,我喜欢选择没有空值的最新日期,然后打印出非空的名称和值。
有没有一种简单的方法可以从这个json输出中选择名称和值字段?
当我做的时候
$data<-json_decode($resp)
print_r($data)
数据的一部分是:
[measurement] => Array
(
[0] => Array
(
[id] => 1132282
[alias] =>example.com
[bucket_data] => Array
(
[0] => Array
(
[name] => 2013-JUN-20 01:23 PM
[id] => 1
[perf_data] => Array
(
[value] => 3.074
[unit] => seconds
)
[avail_data] => Array
(
[value] => 100.00
[unit] => percent
)
[data_count] => Array
(
[value] => 1
[unit] => #
)
)
[1] => Array
(
[name] => 2013-JUN-20 01:28 PM
[id] => 2
[perf_data] => Array
(
[value] => 3.416
[unit] => seconds
)
[avail_data] => Array
(
[value] => 100.00
[unit] => percent
)
[data_count] => Array
(
[value] => 1
[unit] => #
)
)
[2] => Array
(
[name] => 2013-JUN-20 01:33 PM
[id] => 3
[perf_data] => Array
(
[value] => -
[unit] => seconds
)
[avail_data] => Array
(
[value] => -
[unit] => percent
)
[data_count] => Array
(
[value] => -
[unit] => #
)
)
)
答案 0 :(得分:1)
使用内置json_decode功能
$data = json_decode('your_json_here'); //$data will be "stdClass" object
$data = json_decode('your_json_here', true); //$data will be "array" object
然后你可以使用$ data和常规数组/对象
一起操作如果type = stdClass:
$data->measurement[0]->bucket_data[0]->name; // will be equal "2013-JUN-20 11:23 AM"
如果type = array
$data['measurement'][0]['bucket_data'][0]['name']; // will be equal "2013-JUN-20 11:23 AM"