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

查看:107
本文介绍了如何用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()?
  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()?

推荐答案

新答案

修改后的问题:foreach实际上适用于属性以及多值项(数组)

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天全站免登陆