从 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

    }

}

提前致谢。


暮色呼如
浏览 122回答 2
2回答

元芳怎么了

我想它应该像这样简单:$json = json_decode($data, true); print_r($json['body']['txnToken']);

慕斯709654

如果你这样做,它会起作用吗?$json['body']['txnToken']
打开App,查看更多内容
随时随地看视频慕课网APP