I have a JSON string that looks something like this:
{\"addresses\":{\"address\":[{\"@array\":\"true\",\"@id\":\"888888\",\"@uri\":\"xyz\",\"household\":{\"@
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 @
Note that those "@array" and "@id" fields are invalid JSON notation, and technically they lead to unspecified behavior in JSON parsers.
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'];