用 PHP 解析 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19779185/
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 20:00:59  来源:igfitidea点击:

Parse JSON Data with PHP

phpforeachjson

提问by Kakawait

I have parsed JSON data numerous times but for some reason cannot find the correct syntax to use when the data is nested. I am trying to parse the "assets" from this JSON but continue to get a invalid argument supplied foreach() regardless of what I try.

我已多次解析 JSON 数据,但由于某种原因,无法找到嵌套数据时要使用的正确语法。我正在尝试从此 JSON 解析“资产”,但无论我尝试什么,都会继续获得为 foreach() 提供的无效参数。

   "3435":{
      "name":"COLO-1014-SJ1",
      "nickname":"US-SJC-004",
      "type":"Colocated Server",
      "location":"San Jose:55 S Market",
      "assets":{
         "CPU":[
            {
               "model":"Intel E3 1270"
            }
         ],
         "Hard Drives":[
            {
               "model":"Western Digital 500GB RE4 ABYX SATA"
            },
            {
               "model":"Western Digital 500GB RE4 ABYX SATA"
            },
            {
               "model":"Kingston 240GB SSD"
            }
         ],
         "RAM":[
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            }
         ],

I would expect it to be something along the lines of...

我希望它是...

$json = json_decode($jsondata, true);

foreach ($json as $item)
{
    foreach ($item['assets']->RAM as $asset)
    {
        echo $asset->model;
    }

回答by Daniel West

It looks like you have forgot to add the surrounding curly braces around the JSON data. If your JSON data is invalid then the json_decode() function will not work correctly.

看起来您忘记在 JSON 数据周围添加周围的花括号。如果您的 JSON 数据无效,那么 json_decode() 函数将无法正常工作。

I have fixed your JSON code below and this now validates and meets the JSON standard.

我在下面修复了您的 JSON 代码,现在验证并符合 JSON 标准。

{
    "3435": {
        "name": "COLO-1014-SJ1",
        "nickname": "US-SJC-004",
        "type": "Colocated Server",
        "location": "San Jose:55 S Market",
        "assets": {
            "CPU": [
                {
                    "model": "Intel E3 1270"
                }
            ],
            "Hard Drives": [
                {
                    "model": "Western Digital 500GB RE4 ABYX SATA"
                },
                {
                    "model": "Western Digital 500GB RE4 ABYX SATA"
                },
                {
                    "model": "Kingston 240GB SSD"
                }
            ],
            "RAM": [
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                }
            ]
        }
    }
}

If you ever need to check your JSON code you can use a validator such as http://jsonlint.com/

如果您需要检查您的 JSON 代码,您可以使用验证器,例如http://jsonlint.com/

Secondly, your PHP code is also wrong:

其次,你的PHP代码也是错误的:

$json = json_decode($jsondata, true);

foreach ($json as $item)
{
    foreach ($item->assets->RAM as $asset)
    {
        echo $asset->model;
    }
}

You have been trying to access the returned object as an array which will also cause issues with the foreach loop.

您一直试图将返回的对象作为数组访问,这也会导致 foreach 循环出现问题。

回答by ceejayoz

$item->assets, not $item['assets']. Do a print_r($json)so you can see the types of the various parts of the JSON - it'll make them easier to figure out how to access them.

$item->assets,不是$item['assets']。做了print_r($json),所以你可以看到类型JSON的各个部分-它会让他们更容易弄清楚如何访问它们。

回答by Kakawait

From php official documentation: http://php.net/manual/fr/function.json-decode.php

来自 php 官方文档:http: //php.net/manual/fr/function.json-decode.php

The 2nd func arg is for assoc array return. You can use it if you prefer to manipulate assoc array over object.

第二个 func arg 用于关联数组返回。如果您更喜欢在对象上操作关联数组,则可以使用它。

But you actually mix array and object in your loop.

但是您实际上在循环中混合了数组和对象。

If you keep the arg at TRUE, please use $item['assets']['RAM']

如果您将 arg 保持在TRUE,请使用$item['assets']['RAM']

回答by moonwave99

It is parsed as an object, so:

它被解析为一个对象,所以:

foreach ($item -> assets -> RAM as $asset){ ... }