我知道如何在此示例的帮助下使用一个对象解码JSON字符串How to decode a JSON String
但是现在我想用几个对象改进解码JSON字符串,我无法理解如何做到这一点。
以下是一个例子:
{ "inbox": [
{ "firstName": "Brett", "lastName":"McLaughlin" },
{ "firstName": "Jason", "lastName":"Hunter" },
{ "firstName": "Elliotte", "lastName":"Harold" }
],
"sent": [
{ "firstName": "Isaac", "lastName": "Asimov" },
{ "firstName": "Tad", "lastName": "Williams" },
{ "firstName": "Frank", "lastName": "Peretti" }
],
"draft": [
{ "firstName": "Eric", "lastName": "Clapton" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff" }
]
}
答案 0 :(得分:27)
重新修改您的问题:foreach
实际上适用于属性以及多值项(数组)details here。例如,在您的问题中使用JSON字符串:
$data = json_decode($json);
foreach ($data as $name => $value) {
// This will loop three times:
// $name = inbox
// $name = sent
// $name = draft
// ...with $value as the value of that property
}
在属性的主循环中,您可以使用内部循环遍历每个属性指向的数组条目。因此,例如,如果您知道每个顶级属性都有一个数组值,并且每个数组条目都有一个“firstName”属性,则此代码为:
$data = json_decode($json);
foreach ($data as $name => $value) {
echo $name . ':'
foreach ($value as $entry) {
echo ' ' . $entry->firstName;
}
}
...将显示:
inbox: Brett Jason Elliotte sent: Issac Tad Frank draft: Eric Sergei
开始修改 重新评论:
现在我想知道如何使用多个对象解码JSON字符串!
您发布的示例有几个对象,它们只包含在一个包装器对象中。这是JSON的要求;你不能(例如)这样做:
{"name": "I'm the first object"},
{"name": "I'm the second object"}
JSON无效。 有是一个顶级对象。它可能只包含一个数组:
{"objects": [
{"name": "I'm the first object"},
{"name": "I'm the second object"}
]}
...或者您当然可以给出单个对象名称:
{
"obj0": {"name": "I'm the first object"},
"obj1": {"name": "I'm the second object"}
}
结束修改
您的示例是一个包含三个属性的对象,每个属性的值都是一个对象数组。实际上,它与您链接的问题中的示例没有太大区别(它还有一个具有数组值的属性的对象)。
所以:
$data = json_decode($json);
foreach ($data->programmers as $programmer) {
// ...use $programmer for something...
}
foreach ($data->authors as $author) {
// ...use $author for something...
}
foreach ($data->musicians as $musician) {
// ...use $musician for something...
}
答案 1 :(得分:8)
您可以使用 json_decode
功能解码JSON字符串:
$json = <<<JSON
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin" },
{ "firstName": "Jason", "lastName":"Hunter" },
{ "firstName": "Elliotte", "lastName":"Harold" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov" },
{ "firstName": "Tad", "lastName": "Williams" },
{ "firstName": "Frank", "lastName": "Peretti" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff" }
]
}
JSON;
$data = json_decode($json);
然后,要查看数据的外观,可以将其转储:
var_dump($data);
你会看到你有一个包含三个数组的对象,每个数组都包含其他子对象:
object(stdClass)[1]
public 'programmers' =>
array
0 =>
object(stdClass)[2]
public 'firstName' => string 'Brett' (length=5)
public 'lastName' => string 'McLaughlin' (length=10)
1 =>
object(stdClass)[3]
public 'firstName' => string 'Jason' (length=5)
public 'lastName' => string 'Hunter' (length=6)
...
public 'authors' =>
array
0 =>
object(stdClass)[5]
public 'firstName' => string 'Isaac' (length=5)
public 'lastName' => string 'Asimov' (length=6)
...
这意味着您知道如何访问您的数据。
例如,要显示程序员列表,您可以使用:
foreach ($data->programmers as $programmer) {
echo $programmer->firstName . ' ' . $programmer->lastName . '<br />';
}
哪个会得到以下输出:
Brett McLaughlin
Jason Hunter
Elliotte Harold