php - 从 php 中的 json 响应中获取值
问题描述
我是 php 新手。我试图从 json 对象中获取价值。我尝试了很多次,但我未能从 json 中获得价值。我只需要来自 json 数组的“txnToken”。我的代码是
$json = json_decode($data, true);
foreach ($json as $key => $value) {
foreach ($value as $key1 => $value1) {
print_r($key1);
}
}
JSON响应是:
{
"head": {
"responseTimestamp":"1596640639585",
"version":"v1",
"clientId":"WEB",
"signature":"xxxxxxxxxxxxxxxx"
},
"body":{
"resultInfo":{
"resultStatus":"S",
"resultCode":"0000",
"resultMsg":"Success"
},
"txnToken":"xxxxxxxxxxxxx",
"isPromoCodeValid":false,
"authenticated":false
}
}
提前致谢。
解决方案
我想它应该像这样简单:
$json = json_decode($data, true);
print_r($json['body']['txnToken']);
推荐阅读
- datasource - U-SQL 动态外部数据源
- swift - 如何在不标记文件的情况下使用 Swift 以编程方式在 macOS 中添加 Finder 标签?
- r - 忽略 R 函数中的错误 - try() 函数不起作用
- python - python如何在文件夹中组织它的代码?
- monitoring - 如何使用 jconsole 监控本地 Java 进程
- javascript - Node.js - 无赋值要求
- c# - 如何编写具有大小限制(100KB)的json文件,一旦大小交叉,写入新文件?
- python - 如何判断网站是否阻止了我的抓取代码
- c++ - C++填充指向指针**列表的指针指向指针地址
- java - 无法克隆线程 - 为什么?