phpjson_decode删除具有null值的属性


php json_decode removing attributes with null value

我有一个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
            }
        }

属性没有被剥离,您可能正在自己剥离它,就像这里解释的那样:剥离json对象的null值

请参阅您的代码示例:

$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] => ) )