如何用 PHP 中的多个对象解码 JSON 字符串? [英] How to decode a JSON String with several objects in PHP?

查看:23
本文介绍了如何用 PHP 中的多个对象解码 JSON 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您的帮助下,我知道如何使用一个对象解码 JSON 字符串 如何解码 JSON 字符串

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

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

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

这是一个例子:

{ "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. 如何只制作一个 foreach() 到解码上面的 JSON 字符串?
  2. 如何检测对象的名称:收件箱、在此 foreach() 上发送或起草?

推荐答案

新答案

重新修改问题:foreach 实际上适用于属性以及多值项(数组),此处有详细信息.例如,在您的问题中使用 JSON 字符串:

New answer

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

$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"属性,则此代码:

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 know that each of the top-level properties has an array value, and that each array entry has a "firstName" property, this code:

$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 字符串!

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

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

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

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

该 JSON 无效.必须是一个单一的顶级对象.它可能只包含一个数组:

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

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

结束编辑

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

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

所以:

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

这篇关于如何用 PHP 中的多个对象解码 JSON 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆