本文介绍了如何在PHP中解码JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的JSON字符串:
I have a JSON string that looks something like this:
{"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才能对此进行解码并将address1
,address2
,address3
,city
和postalCode
放入会话变量中?
What would be the PHP to decode this and place address1
, address2
, address3
, city
, and postalCode
into session variables?
到目前为止,我已经尝试过了,但是没有用:
So far I tried this but it's not working:
$results = json_decode(strstr($address, '{"addresses":{"address":[{'), true);
$_SESSION['address1'] = $results['address']['address1'];
谢谢!
推荐答案
print_r
是您了解JSON结构的朋友.
print_r
is your friend for figuring out JSON structure.
<?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
)
这篇关于如何在PHP中解码JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!