How to decode a JSON string in PHP?

后端 未结 9 1112
挽巷
挽巷 2020-12-02 02:18

I have a JSON string that looks something like this:

{\"addresses\":{\"address\":[{\"@array\":\"true\",\"@id\":\"888888\",\"@uri\":\"xyz\",\"household\":{\"@         


        
相关标签:
9条回答
  • 2020-12-02 02:44

    json_decode($jsonData) returns an object btw, not an array.

    For example:

    stdClass Object
    (
        [addresses] => stdClass Object
            (
                [address] => Array
                    (
                        [0] => stdClass Object
                            (
                                [@array] => true
                                [@id] => 888888
                                [@uri] => xyz
                                [household] => stdClass Object
                                    (
                                        [@id] => 44444
                                        [@uri] => xyz
                                    )
    
                                [person] => stdClass Object
                                    (
                                        [@id] => 
                                        [@uri] => 
                                    )
    
                                [addressType] => stdClass Object
                                    (
                                        [@id] => 1
                                        [@uri] => xyz
                                        [name] => Primary
                                    )
    
                                [address1] => xyz
                                [address2] => 
                                [address3] => 
                                [city] => xyz
                                [postalCode] => 111111
                            )
    
                    )
    
            )
    
    )
    

    Ways to access data:

    $object = json_decode($jsonString);
    $object->addresses->address[0]; // First address object
    $object->addresses->address[0]->{"@array"}; // Not good way to access object property (damn @)
    $object->addresses->address[0]->address1;
    $object->addresses->address[0]->addressType->{"@id"}; // Again damn @
    
    0 讨论(0)
  • 2020-12-02 02:56

    Note that those "@array" and "@id" fields are invalid JSON notation, and technically they lead to unspecified behavior in JSON parsers.

    0 讨论(0)
  • 2020-12-02 02:57

    Why not decode the whole JSON string and then get what you need?

    $address = '{"addresses":{"address":[{"@array":"true","@id":"888888","@uri":"xyz","household":{"@id":"44444","@uri":"xyz"},"person":{"@id":"","@uri":""},"addressType":{"@id":"1","@uri":"xyz","name":"Primary"},"address1":"xyz","address2":null,"address3":null,"city":"xyz","postalCode":"111111"}]}}';
    $results = json_decode($address, true);
    $address = $results['addresses']['address'][0];
    print $address['address1'];
    print $address['address2'];
    print $address['postalCode'];
    
    0 讨论(0)
提交回复
热议问题