从json Php获取元素的访问权限

时间:2018-11-30 10:27:48

标签: php json

我有一个如下的Json,其中我必须在php的响应中仅获取transId。请帮忙,因为我是php新手

{
    "type": null,
    "requestuid": null,
    "orderId": "anand12345",
    "status": "SUCCESS",
    "statusCode": "SUCCESS",
    "statusMessage": "SUCCESS",
    "response": {
        "transId": "1408544"
    },
    "metadata": "Testing Data"
}

2 个答案:

答案 0 :(得分:3)

$json = '{
 "type": null,
 "requestuid": null,
 "orderId": "anand12345",
 "status": "SUCCESS",
 "statusCode": "SUCCESS",
 "statusMessage": "SUCCESS",
 "response": {"transId": "1408544"},
 "metadata": "Testing Data"
}';

$arr = json_decode($json,true);
$transId = $arr['response']['transId'];

答案 1 :(得分:1)

读取文件并将其内容存储为字符串(这里我直接声明了存储在$json中的字符串) 使用json_decode php function,您将获得一个包含所有数据的对象,而只需使用字段名称来访问数据

$json = '{
"type": null,
"requestuid": null,
"orderId": "anand12345",
"status": "SUCCESS",
"statusCode": "SUCCESS",
"statusMessage": "SUCCESS",
"response": {
    "transId": "1408544"
},
"metadata": "Testing Data"

}';


$decoded = json_decode($json);
echo $decoded->response->transId;