如何用 PHP 中的多个对象解码 JSON 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2560096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:57:04  来源:igfitidea点击:

How to decode a JSON String with several objects in PHP?

phpobjectjson

提问by ilnur777

I know how to decode a JSON string with one object with your help from this example How to decode a JSON String

我知道如何在此示例的帮助下使用一个对象解码 JSON 字符串How to decode a JSON String

But now I would like to improve decoding JSON string with several objects and I can't understand how to do it.

但是现在我想用几个对象改进解码 JSON 字符串,但我不明白该怎么做。

Here is an example:

下面是一个例子:

{ "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" }
 ]
}
  1. How to make just one foreach() to decode above JSON string?
  2. How to detect object's names: inbox, sent or draft on this foreach()?
  1. 如何只用一个 foreach() 来解码上面的 JSON 字符串?
  2. 如何在此 foreach() 上检测对象的名称:收件箱、已发送或草稿?

回答by T.J. Crowder

New answer

新答案

Re your revised question: foreachactually works with properties as well as with many-valued items (arrays), details here. So for instance, with the JSON string in your question:

关于您修改后的问题:foreach实际上适用于属性以及多值项(数组),详情请点击此处。例如,在您的问题中使用 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
}

Within your main loop over the properties, you can use an inner loop to go over the array entries each property points to. So for instance, if you knowthat each of the top-level properties has an array value, and that each array entry has a "firstName" property, this code:

在属性的主循环中,您可以使用内部循环来遍历每个属性指向的数组条目。例如,如果您知道每个顶级属性都有一个数组值,并且每个数组条目都有一个“firstName”属性,则此代码:

$data = json_decode($json);
foreach ($data as $name => $value) {
    echo $name . ':'
    foreach ($value as $entry) {
        echo '  ' . $entry->firstName;
    }
}

...will show:

...将会呈现:

inbox:
  Brett
  Jason
  Elliotte
sent:
  Issac
  Tad
  Frank
draft:
  Eric
  Sergei

Old answer(s)

旧答案

Begin editRe your comment:

开始编辑回复您的评论:

Now I would like to know how to decode JSON string with several objects!

现在我想知道如何使用多个对象解码 JSON 字符串!

The example you posted doeshave several objects, they're just all contained within one wrapper object. This is a requirement of JSON; you cannot (for example) do this:

您发布的示例确实有多个对象,它们都包含在一个包装器对象中。这是JSON的要求;你不能(例如)这样做:

{"name": "I'm the first object"},
{"name": "I'm the second object"}

That JSON is not valid. There hasto be a single top-level object. It might just contain an array:

该 JSON 无效。有是一个顶级对象。它可能只包含一个数组:

{"objects": [
    {"name": "I'm the first object"},
    {"name": "I'm the second object"}
]}

...or of course you can give the individual objects names:

...或者当然你可以给各个对象命名:

{
    "obj0": {"name": "I'm the first object"},
    "obj1": {"name": "I'm the second object"}
}

End edit

结束编辑

Your example is one object containing three properties, the value of each of which is an array of objects. In fact, it's not much different from the example in the question you linked (which also has an object with properties that have array values).

您的示例是一个包含三个属性的对象,每个属性的值都是一个对象数组。事实上,它与您链接的问题中的示例没有太大区别(它也有一个具有数组值的属性的对象)。

So:

所以:

$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...
}

回答by Pascal MARTIN

You can use the json_decodefunction to decode the JSON string :

您可以使用该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);


Then, to see what the data looks like, you can dump it :


然后,要查看数据的样子,您可以转储它:

var_dump($data);


And you'll see you have an object containing three arrays, each one containing other sub-objects :


你会看到你有一个包含三个数组的对象,每个数组包含其他子对象:

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

Which means you know how to access your data.

这意味着您知道如何访问您的数据。


For example, to display the list of the programmers, you could use :


例如,要显示程序员列表,您可以使用:

foreach ($data->programmers as $programmer) {
  echo $programmer->firstName . ' ' . $programmer->lastName . '<br />';
}


Which would get you the following output :


这会让你得到以下输出:

Brett McLaughlin
Jason Hunter
Elliotte Harold