>我向服务器发送了一个curl请求,并得到以下结果:
{ "data": [
{ "state_msg": "Canceled",
"dpc": null,
"mgcp_call_ids": null,
"code": 487,"mgcp_mgc_ip": null,
"dst_codecs": "G729,telephone-event,H264,H263",
"megaco_mg_ip": null,"src_codecs":
"G729,PCMU,telephone-event,H264,H263",
"q850_state_details": null,
"pid":1383031680,
"call_time": null
.......
如何将其解析为可读字段? 我正在寻找类似foreach循环或类似的东西。
如果这是XML,我可以做
$response->data->state_msg.
显然,这在这里行不通。 我能做什么?
这是一个
JSON响应。通过 json_decode
传递字符串以接收对象并像循环访问任何其他对象一样对其进行迭代。
这样做:
$json_string = '{"data": {"state_msg": "Canceled", "dpc": null, "mgcp_call_ids": null, "code": 487,"mgcp_mgc_ip": null, "dst_codecs": "G729,telephone-event,H264,H263", "megaco_mg_ip": null,"src_codecs": "G729,PCMU,telephone-event,H264,H263", "q850_state_details": null, "pid":1383031680, "call_time": null}');
$obj = json_decode($json_string);
它会将您的 JSON 转换为数组,您可以像这样阅读它
echo $obj->data->state_msg;