如何解码PHP中的JSON字符串?

时间:2009-08-12 21:21:45

标签: php json

我有一个看起来像这样的JSON字符串:

{"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"}]}}

解码此问题的PHP是什么,并将address1address2address3citypostalCode放入会话变量?

到目前为止,我尝试了这个但是它无效:

$results = json_decode(strstr($address, '{"addresses":{"address":[{'), true);
$_SESSION['address1'] = $results['address']['address1'];

谢谢!

9 个答案:

答案 0 :(得分:9)

print_r是你搞定JSON结构的朋友。

<?php

$addresses = json_decode('{"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"}]}}');

$_SESSION['address1'] = $addresses->addresses->address[0]->address1;
$_SESSION['address2'] = $addresses->addresses->address[0]->address2;
$_SESSION['address3'] = $addresses->addresses->address[0]->address3;
$_SESSION['city'] = $addresses->addresses->address[0]->city;
$_SESSION['postalCode'] = $addresses->addresses->address[0]->postalCode;

print_r($_SESSION);

结果:

Array
(
    [address1] => xyz
    [address2] => 
    [address3] => 
    [city] => xyz
    [postalCode] => 111111
)

答案 1 :(得分:3)

json_decode会将json格式的字符串解码为PHP 对象

试试这个:

$results = json_decode($address);
$results['address1'] = $results->addresses->address[0]->address1;
$results['address2'] = $results->addresses->address[0]->address2;
$results['address3'] = $results->addresses->address[0]->address3;
$results['city'] = $results->addresses->address[0]->city;
$results['postalCode'] = $results->addresses->address[0]->postalCode;

编辑 - 更新,我最初误读了你的JSON。

答案 2 :(得分:2)

请注意,那些“@array”和“@id”字段是无效的JSON表示法,从技术上讲,它们会导致JSON解析器中出现未指定的行为。

答案 3 :(得分:1)

为什么不解码整个JSON字符串然后得到你需要的东西?

$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'];

答案 4 :(得分:0)

也许尝试$ results ['addresses'] ['address'] ['address1'];

不确定为什么要使用strstr。但在这种情况下看起来并没有改变任何东西。

答案 5 :(得分:0)

你可以使用print_r输出$ results来找出对象输出的确切内容。

答案 6 :(得分:0)

如果您执行数组的print_r,则会看到布局如何:

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
            )
        )
    )
)

答案 7 :(得分:0)

json_decode($ jsonData)返回一个对象btw,而不是一个数组。

例如:

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
                        )

                )

        )

)

访问数据的方法:

$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 @

答案 8 :(得分:0)

这个会将所有标量和空值放入会话中,其中key不以@开头     

$jsonString = '{"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"}]}}';

$result = json_decode($jsonString);

// will put *all* scalar and null values into session where key does not begin with a @
foreach($result->addresses->address[0] as $key=>$value) {
    if (substr($key, 0, 1) != '@'  && (is_scalar($value) || is_null($value)) ) {
        $_SESSION[$key] = $value;
    } 
}

print_r($_SESSION);

?>