【问题标题】:php json_decode removing attributes with null valuephp json_decode 删除具有空值的属性
【发布时间】:2014-10-14 14:55:34
【问题描述】:

我有一个 Json 字符串,我正在使用 php 的 json_decode 对其进行解码。

字符串

            "address": {
                "address": null,
                "postalCode": null,
                "phoneNumber": "",
                "city": null
            }

当我解码得到的字符串时

            ["address"]=>
                  array(1) {
                  ["phoneNumber"]=>
                       string(0) ""

它本质上是去除带有 null 值的属性,即地址、城市。我可以防止这种情况发生吗?

完整的 JSON

            {"cost": null,
            "receiptNumber": null,
            "receiptType": null,
            "labNo": 596726,
            "parentLabNo": 0,
            "investigation": "BS for mps",
            "patient": {
                "id": 168967,
                "fullName": "UVOGIN",
                "dateOfBirth": "1972-04-04 00:00:00",
                "gender": "Male"
            },
            "address": {
                "address": null,
                "postalCode": null,
                "phoneNumber": "",
                "city": null
            }
        }

【问题讨论】:

标签: php json


【解决方案1】:

属性没有被剥离,你可能会像这里解释的那样自己剥离它:strip null values of json object

查看您的代码示例:

$test = '{"address": {
            "address": null,
            "postalCode": null,
            "phoneNumber": "",
            "city": null
        }}';

$test_decoded = json_decode($test,true);
print_r($test_decoded);

//outputs as expected:
//Array ( [address] => Array ( [address] => [postalCode] => [phoneNumber] => [city] => ) )

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多