获取json值并将其分配给变量

时间:2017-10-18 20:40:08

标签: php arrays json var

嗨,我有现在的json

{
    "merchantOrderId": "123",
    "customer": {
        "name": "Fulano de Tal",
        "email": null,
        "birthDate": null,
        "identity": null,
        "identityType": null,
        "address": null,
        "deliveryAddress": null
    },
    "payment": {
        "serviceTaxAmount": 0,
        "installments": 1,
        "interest": 0,
        "capture": false,
        "authenticate": false,
        "recurrent": false,
        "recurrentPayment": null,
        "creditCard": {
            "cardNumber": "545301******6167",
            "holder": "Fulano de Tal",
            "expirationDate": "05\/2018",
            "securityCode": null,
            "saveCard": false,
            "brand": "Master",
            "cardToken": null
        },
        "debitCard": null,
        "authenticationUrl": null,
        "tid": "10927989166K6L145T3B",
        "proofOfSale": "006015",
        "authorizationCode": null,
        "softDescriptor": "",
        "returnUrl": null,
        "provider": "Cielo",
        "paymentId": "c1a253ce-0f9a-4bc1-b8d7-43cf18f023bf",
        "type": "CreditCard",
        "amount": 15700,
        "receivedDate": "2017-10-18 18:18:51",
        "capturedAmount": null,
        "capturedDate": null,
        "currency": "BRL",
        "country": "BRA",
        "returnCode": "GA",
        "returnMessage": "Autorizacao negada",
        `"status":3`,
        "links": [{
            "Method": "GET",
            "Rel": "self",
            "Href": "https:\/\/apiquery.cieloecommerce.cielo.com.br\/1\/sales\/c1a253ce-0f9a-4bc1-b8d7-43cf18f023bf"
        }],
        "extraDataCollection": null,
        "expirationDate": null,
        "url": null,
        "number": null,
        "barCodeNumber": null,
        "digitableLine": null,
        "address": null
    }
}

如何获取status的值并将其分配给变量?

2 个答案:

答案 0 :(得分:1)

您需要将此JSON转换为具有函数json_decode的数组。然后你可以轻松解析它

$data = json_decode($json, true);
$status = $data['payment']['status'];

答案 1 :(得分:1)

假设您将JSON存储为名为$json的变量中的字符串,您可以使用 json_decode ,然后访问payment键,然后status密钥包含以下内容:

$json = '{ ... }';
$status = json_decode($json)->payment->status;
echo $status; // 3

可以看到 here

希望这有帮助! :)