doumeng06063991
2014-08-06 11:15
浏览 824

使用PHP解析多维嵌套JSON

I feel like i am slightly insane, and I have certainly read the docs on this. I am completely unable to echo out various objects in a JSON array in PHP. I am not sure what I'm doing wrong, but I'm ripping my hair out...

Here is my JSON array:

{
    "photos": {
        "page": 1,
        "pages": 1569045,
        "perpage": 1,
        "total": "1569045",
        "photo": [
            {
                "id": "14842817422",
                "owner": "23432140@N06",
                "secret": "c37cfa1914",
                "server": "3864",
                "farm": 4,
                "title": "pizza",
                "ispublic": 1,
                "isfriend": 0,
                "isfamily": 0
            }
        ]
    },
    "stat": "ok"
}

I know this is simple, but I can't get it right. I would like to echo out four different values.

This is what I have been trying:

$photoId = $jsonDecoded['photos']['photo'][0]['id'];
$photoSecret = $jsonDecoded['photos']['photo'][0]['secret'];
$photoServer = $jsonDecoded['photos']['photo'][0]['server'];
$photoFarm = $jsonDecoded['photos']['photo'][0]['farm'];

I know this seems newbie. Please help...

Best,

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

我觉得我有点疯了,我当然已经阅读过这方面的文档了。 我完全无法在PHP中回显JSON数组中的各种对象。 我不确定我做错了什么,但是我正在把我的头发撕掉......

这是我的JSON数组:

  {
“photos”:{
“page”:1,
“pages”:1569045,
“perpage”:1,
“total”:“1569045  “,
”照片“:[
 {
”id“:”14842817422“,
”所有者“:”23432140 @ N06“,
”秘密“:”c37cfa1914“,
”服务器“:  “3864”,
“farm”:4,
“title”:“pizza”,
“ispublic”:1,
“isfriend”:0,
“isfamily”:0 
} 
  ] 
},
“stat”:“ok”
} 
   
 
 

我知道这很简单,但我说得不对。 我想回应四个不同的值。

这就是我一直在尝试的:

   $ photoId = $ jsonDecoded ['photos'] ['photo'] [0] ['id']; 
 $ photoSecret = $ jsonDecoded ['photos'] ['photo'] [0] ['secret'];  
 $ photoServer = $ jsonDecoded ['photos'] ['photo'] [0] ['server']; 
 $ photoFarm = $ jsonDecoded ['photos'] ['photo'] [0] ['farm'  ]; 
   
 
 

我知道这似乎是新手。 请帮助......

最好,

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsrbb20862 2014-08-06 11:27
    已采纳

    The problem is that you have both objects and arrays in your json, but are using array syntax in your php.

    There are two ways to fix this, 1st simply set the second parameter of json_decode to true:

    json_decode($json, true);
    

    This will create a multidimentional array you can access as suggested in your question, eg:

    $photoId = $jsonDecoded['photos']['photo'][0]['id'];
    

    Alertinitivly you can use object property syntax on your existing $jsonDecoded:

    $photoId = $jsonDecoded->photos->photo[0]->id;
    
    打赏 评论
  • douxin8749 2014-08-06 11:20

    if there are multiple photo sub arrays then you can do like this.

    //this will create array instead of object
    $jsonDecoded = json_decode($your_feed_data,true);
    
    foreach($jsonDecoded['photos']['photo'] as $sub_array){
    
    $photoId = $sub_array['id'];
    $photoSecret = $sub_array['secret'];
    $photoServer = $sub_array['server'];
    $photoFarm = $sub_array['farm'];
    
    }
    
    打赏 评论

相关推荐 更多相似问题