douyu7879
2013-11-04 23:21
浏览 121

使用PHP解析JSON数据

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.

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

图片转代码服务由CSDN问答提供 功能建议

我已多次解析JSON数据但由于某种原因无法找到嵌套数据时使用的正确语法。 我试图从这个JSON解析“资产”但是继续得到一个无效的参数foreach(),无论我尝试什么。

 “3435”:{
  “name”:“COLO-1014-SJ1”,
“昵称”:“US-SJC-004”,
“类型”:“Colocated Server”,
“location”:“San Jose:55 S Market  “,
”“资产”:{
“CPU”:[
 {
“型号”:“Intel E3 1270”
} 
],
“硬盘”:[
 {
  “型号”:“Western Digital 500GB RE4 ABYX SATA”
},
 {
“型号”:“Western Digital 500GB RE4 ABYX SATA”
},
 {
“型号”:“金士顿240GB SSD  “
} 
 
],
”“RAM”:[
 {
“型号”:“Super Talent 4GB DDR3 1333 ECC”
},
 {
“型号”:“Super Talent 4GB DDR3  1333 ECC“
},
 {
”model“:”Super Talent 4GB DDR3 1333 ECC“
  },
 {
“model”:“Super Talent 4GB DDR3 1333 ECC”
} 
],
   
 
 

我希望它是某种东西 沿着...的行...

  $ json = json_decode($ jsondata,true); 
 
foreach($ json as $ item)
 {
 foreach(  $ item ['assets']  - > RAM为$ asset)
 {
 echo $ asset-> model; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • donglijuan8227 2013-11-05 08:48
    已采纳

    From php official documentation: 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.

    But you actually mix array and object in your loop.

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

    点赞 打赏 评论
  • doudao7511 2013-11-04 23:24

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

    点赞 打赏 评论
  • douyi9705 2013-11-04 23:24

    It is parsed as an object, so:

    foreach ($item -> assets -> RAM as $asset){ ... }
    
    点赞 打赏 评论
  • dsxz84851 2013-11-04 23:26

    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.

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

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

    Secondly, your PHP code is also wrong:

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

    点赞 打赏 评论

相关推荐 更多相似问题