php json_decode删除具有null值的属性

时间:2014-10-14 14:55:34

标签: php json

我有一个Json字符串,我正在使用php的json_decode解码它。

字符串

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

当我解码字符串时,我得到了

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

它实际上剥离了带有null的属性作为值,即地址,城市。我能否防止这种情况发生。

COMPLETE 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
            }
        }

1 个答案:

答案 0 :(得分: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] => ) )