如何在PHP中从json响应中回显特定值
我想知道从这样的JSON响应中获取值的最佳方法是什么如何在PHP中从json响应中回显特定值,php,json,rest,Php,Json,Rest,我想知道从这样的JSON响应中获取值的最佳方法是什么 { "customer": { "link": { "url": "https://api.neteller.com/v1/customers/CUS_0d676b4b-0eb8-4d78-af25-e41ab431e325", "rel": "customer", "method": "GET" } }, "transaction": { "merchantRefId": "20140203113703",
{
"customer": {
"link": {
"url": "https://api.neteller.com/v1/customers/CUS_0d676b4b-0eb8-4d78-af25-e41ab431e325",
"rel": "customer",
"method": "GET"
}
},
"transaction": {
"merchantRefId": "20140203113703",
"amount": 2500,
"currency": "EUR",
"id": "176391453448397",
"createDate": "2014-02-03T18:50:48Z",
"updateDate": "2014-02-03T18:50:51Z",
"status": "Accepted",
"fees": [
{
"feeType": "Service_fee",
"feeAmount": 71,
"feeCurrency": "EUR"
}
]
},
"links": [
{
"url": "https://api.neteller.com/v1/payments/176391453448397",
"rel": "self",
"method": "GET"
}
]
}
我已将响应存储在变量中:
$content = json_decode($data['content']);
如何在PHP中回显这些值?比如说,我想买一辆二手车。谢谢 我在这里测试了这个:
$content = json_decode($data['content'], true);
echo $content['transaction']['merchantRefId'];
echo$content->transaction->merchantRefId代码>
json\u decode()
生成标准类的对象。然后使用对象表示法访问该对象的属性
或者,您可以使用
$content=json\u decode($data['content'],true)代码>
你会得到一个关联数组,而不是一个对象。然后您可以通过元素名访问它,如
echo$content['transaction']['merchantRefId']
json\u decode()
将构建一个关联数组来映射您的json,因此如果您这样做
echo "<PRE>"; print_r($content);
使用print\u r()
函数检查它是如何映射的,以便您知道它是如何精确映射的。起初,我举例说明的方式似乎是正确的。啊,是的,似乎我必须将json_decode设置为true!谢谢将给出+1,但还没有15个代表:(虽然这个答案是有效的,但只有代码的答案不如有解释的答案有用。请参阅:简洁是可以接受的,但更完整的解释更好。json_decode()
不构建关联数组,除非您使用第二个参数boolean true告诉它。请参阅我的答案,其中涵盖了这两种情况。
$merchantRefId = $content['transaction']['merchantRefId'];