Php 如何解码JSON字符串
大家好!我能请你帮我解码这个JSON代码吗Php 如何解码JSON字符串,php,json,Php,Json,大家好!我能请你帮我解码这个JSON代码吗 $json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from
$json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';
我想将上述结构组织为:
注1:
文件夹:收件箱
From(From):
日期:
时间:
utcOffsetSeconds:
最近(地址):
最近者(姓名):
状态(deliveryStatus):
正文:
注2:
提前感谢您您可以使用该函数来解码JSON字符串:
$json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';
$data = json_decode($json);
var_dump($data);
你会得到这样的结果:
object(stdClass)[1]
public 'inbox' =>
array
0 =>
object(stdClass)[2]
public 'from' => string '55512351' (length=8)
public 'date' => string '29/03/2010' (length=10)
public 'time' => string '21:24:10' (length=8)
public 'utcOffsetSeconds' => int 3600
public 'recipients' =>
array
0 =>
object(stdClass)[3]
public 'address' => string '55512351' (length=8)
public 'name' => string '55512351' (length=8)
public 'deliveryStatus' => string 'notRequested' (length=12)
public 'body' => string 'This is message text.' (length=21)
1 =>
object(stdClass)[4]
public 'from' => string '55512351' (length=8)
public 'date' => string '29/03/2010' (length=10)
public 'time' => string '21:24:12' (length=8)
public 'utcOffsetSeconds' => int 3600
public 'recipients' =>
array
0 =>
object(stdClass)[5]
public 'address' => string '55512351' (length=8)
public 'name' => string '55512351' (length=8)
public 'deliveryStatus' => string 'notRequested' (length=12)
public 'body' => string 'This is message text.' (length=21)
....
....
foreach ($data->inbox as $note) {
echo '<p>';
echo 'From : ' . htmlspecialchars($note->from) . '<br />';
echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
echo '</p>';
}
现在您知道了数据的结构,可以对其进行迭代;例如,您可以使用如下内容:
object(stdClass)[1]
public 'inbox' =>
array
0 =>
object(stdClass)[2]
public 'from' => string '55512351' (length=8)
public 'date' => string '29/03/2010' (length=10)
public 'time' => string '21:24:10' (length=8)
public 'utcOffsetSeconds' => int 3600
public 'recipients' =>
array
0 =>
object(stdClass)[3]
public 'address' => string '55512351' (length=8)
public 'name' => string '55512351' (length=8)
public 'deliveryStatus' => string 'notRequested' (length=12)
public 'body' => string 'This is message text.' (length=21)
1 =>
object(stdClass)[4]
public 'from' => string '55512351' (length=8)
public 'date' => string '29/03/2010' (length=10)
public 'time' => string '21:24:12' (length=8)
public 'utcOffsetSeconds' => int 3600
public 'recipients' =>
array
0 =>
object(stdClass)[5]
public 'address' => string '55512351' (length=8)
public 'name' => string '55512351' (length=8)
public 'deliveryStatus' => string 'notRequested' (length=12)
public 'body' => string 'This is message text.' (length=21)
....
....
foreach ($data->inbox as $note) {
echo '<p>';
echo 'From : ' . htmlspecialchars($note->from) . '<br />';
echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
echo '</p>';
}
收件人属性似乎是数组,请尝试以下操作:
$json='{“收件箱”:[{“发件人”:“55512351”,“日期”:“29\/03\/2010”,“时间”:“21:24:10”,“utcOffsetSeconds”:3600,“收件人”:[{“地址”:“55512351”,“姓名”:“55512351”,“交付状态”:“未请求”}],“正文”:“这是邮件文本”。},{“发件人”:“55512351”,“日期”:“29\/03\/2010”,“时间”:“21:24:12”,“utcOffsetSeconds”:3600,“收件人”:“收件人”:“地址”:“55512351”,“姓名”:“55512351”,“deliveryStatus”:“notRequested”}],“body”:“This is message text.”,{“from”:“55512351”,“date”:“29\/03\/2010”,“time”:“21:24:13”,“utcOffsetSeconds”:3600,“recipients”:[{“address”:“55512351”,“deliveryStatus”:“notRequested”}],“body”:“This is message text.”,},{“from”:“55512351”,“date”:“29\/03\/2010”,“time”:“21:24:13”,”utcOffsetSeconds“:3600,“收件人”:[{“地址”:“55512351”,“姓名”:“55512351”,“传递状态”:“未请求”}],“正文”:“这是邮件文本。”}]}”;
$data=json_decode($json);
打印(数据);
foreach($data->收件箱为$note)
{
回声“”;
echo“From:”.htmlspecialchars($note->From)。“
”;
回显“日期:”.htmlspecialchars($note->Date)。“
”;
echo“Time:”.htmlspecialchars($note->Time)。“
”;
回显“Body:”.htmlspecialchars($note->Body)。“
”;
foreach($note->recipients as$recipient)
{
回显“收件人(地址):.htmlspecialchars($recipient->address)。”
;
echo“To(name):”.htmlspecialchars($recipient->name)。“
”;
回显“状态:”.htmlspecialchars($recipient->deliveryStatus)。“
”;
}
}
在此处输入代码
Pascal MARTIN,谢谢你的快速反应!但是这个乘法数组中的问题是。我真的不能用所有这些数组来操作,也不能像上面我想做的那样做一个简单的结构。请,我问你,你能演示一下我应该如何操作所有这些数组吗?你能用我不知道的foreach()来演示它吗还是什么?谢谢你!@ilnur777:如果你不知道foreach
,请阅读文档:.for
和foreach
循环是处理数组时必不可少的工具。帕斯卡·马丁,你救了我的命和我的神经!!!非常感谢。现在我真的可以理解如何在JSON上操作数组了!@ilnur777:不要忘记将Pascal的答案标记为正确答案:-)如果总是有一个且只有一个收件人,类似于$note->recipients[0]->name
的方法应该可以工作;如果有时有多个收件人,您可以使用foreach
循环在$note->recipients
enter code here
<?php
$subject = file_get_contents('http://example.com/subject.php');
$subject_arr = json_decode($subject);
$my = $subject_arr->info;
for($i=0;$i<=1000;$i++){
echo $my[$i]->subject_name;
echo "<br>";
}
echo $my[0]->subject_name;
?>
<?php
$subject = file_get_contents('http://example.com/subject.php');
$subject_arr = json_decode($subject);
$my = $subject_arr->info;
for($i=0;$i<=1000;$i++){
echo $my[$i]->subject_name;
echo "<br>";
}
?>