如何解析嵌套的json到php

时间:2015-05-06 15:08:18

标签: php json decode

{
  "status": "success",
  "photos": [
    {
      "width": 640,
      "height": 640,
      "tags": [
        {
          "uids": [

          ],
          "label": null,
          "confirmed": false,
          "manual": false,
          "width": 27.81,
          "height": 27.81,
          "yaw": 0,
          "roll": -12,
          "pitch": 0,
          "attributes": {
            "face": {
              "value": "true",
              "confidence": 69
            }
          },
          "points": null,
          "similarities": null,
          "tid": "TEMP_F@059dfdfa5f464b1f3bc1c351013100c0_a8ecc6434914f_47.66_30.00_0_1",
          "recognizable": true,
          "threshold": 49,
          "center": {
            "x": 47.66,
            "y": 30
          },
          "eye_left": {
            "x": 53.12,
            "y": 22.19,
            "confidence": 62,
            "id": 449
          },
          "eye_right": {
            "x": 38.91,
            "y": 25,
            "confidence": 61,
            "id": 450
          },
          "mouth_center": {
            "x": 48.91,
            "y": 40.31,
            "confidence": 58,
            "id": 615
          },
          "nose": {
            "x": 47.66,
            "y": 33.12,
            "confidence": 58,
            "id": 403
          }
        },
        {
          "uids": [
            {
              "uid": "jasonn@mydoc",
              "confidence": 100
            }
          ],
          "label": null,
          "confirmed": true,
          "manual": false,
          "width": 5,
          "height": 5,
          "yaw": 0,
          "roll": 3,
          "pitch": 0,
          "attributes": {
            "face": {
              "value": "true",
              "confidence": 52
            }
          },
          "points": null,
          "similarities": null,
          "tid": "01520262_a8ecc6434914f",
          "recognizable": true,
          "threshold": 49,
          "center": {
            "x": 52.81,
            "y": 95.31
          },
          "eye_left": {
            "x": 54.38,
            "y": 94.53,
            "confidence": 17,
            "id": 449
          },
          "eye_right": {
            "x": 51.72,
            "y": 94.38,
            "confidence": 50,
            "id": 450
          },
          "mouth_center": {
            "x": 53.12,
            "y": 96.72,
            "confidence": 24,
            "id": 615
          },
          "nose": {
            "x": 53.12,
            "y": 95.62,
            "confidence": 51,
            "id": 403
          }
        }
      ]
    }
  ],
  "usage": {
    "used": 15,
    "remaining": 85,
    "limit": 100,
    "reset_time": 1430928036,
    "reset_time_text": "Wed, 6 May 2015 16:00:36 +0000"
  },
  "operation_id": "993267fe68ce4f02a6c239d8d45faa9d"
}

这是从url生成的json文件,我尝试使用

对其进行解码
$json = file_get_contents('url');
$obj = json_decode($json,true);

我能够获得第一个元素

echo $obj['success'];

但是如何在照片中访问以下部分。 我搜索了很多例子,但都无法工作。 有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

正如您所看到的print_r($obj)

  • “照片”是一个数组
  • “photos”数组的字段“tags”是一个数组
  • “确认”是“标签”-array
  • 的字段

所以这样做

$obj = json_decode($json,true);

echo $obj['status']."<br />";

foreach($obj['photos'] as $k => $v) {
    echo "Photo $k : <br />";

    foreach($v['tags'] as $kt => $vt) {
        echo " Tag $kt confirmed : ". (($vt['confirmed'])?"TRUE":"FALSE"). "<br />";
    }
}

输出是:

success
Photo 0 : 
Tag 0 confirmed : FALSE
Tag 1 confirmed : TRUE

要直接访问它,你应该这样做:

// access confirmed field of first tag (first photo)
echo $obj['photos'][0]['tags'][0]['confirmed'];

// access confirmed field of second tag (first photo)
echo $obj['photos'][0]['tags'][1]['confirmed'];