php从json编码中获取值

时间:2012-09-14 17:08:30

标签: php json

我有一个url传递参数使用json_encode每个值如下:

$json = array
(
    'countryId' => $_GET['CountryId'],
    'productId' => $_GET['ProductId'],
    'status'    => $_GET['ProductId'],
    'opId'      => $_GET['OpId']
);

echo json_encode($json);

返回结果为:

{  
  "countryId":"84",
  "productId":"1",
  "status":"0",
  "opId":"134"
}

我可以使用json_decode来解析每个值以进行进一步的数据处理吗?

感谢。

3 个答案:

答案 0 :(得分:61)

如果第二个值为真,

json_decode()将返回一个对象或数组:

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}';
$json = json_decode($json, true);
echo $json['countryId'];
echo $json['productId'];
echo $json['status'];
echo $json['opId'];

答案 1 :(得分:9)

json_decode将返回最初编码的相同数组。 对于instanse,如果你

$array = json_decode($json, true);
echo $array['countryId'];

OR

$obj= json_decode($json);

echo $obj->countryId;

这两者都会回应84。 我认为json_encode和json_decode函数名称是不言自明的......

答案 2 :(得分:-2)

我如何从中引用assetId

{
    "head": {
        "mid": null,
        "clientId": null,
        "version": "8.2.0",
        "requestId": "9427276e6f5fa6ff1574079193065",
        "responseTimestamp": "1574079199020"
    },
    "body": {
        "extendInfo": null,
        "resultInfo": {
            "resultStatus": "S",
            "resultCodeId": "0000",
            "resultCode": "SUCCESS",
            "resultMsg": "Success"
        },
        "refundAccounts": [
            {
                "maskedAccountNumber": "919932**2589",
                "assetId": "120300000032338407602",
                "accountType": null,
                "bankName": "Paytm Payments Bank Ltd",
                "holderName": {
                    "firstName": "Akash",
                    "lastName": "Biswas"
                }
            }
        ]
    }
}