简体   繁体   English

如何将嵌套的json解析为php

[英]How to parse nested json to php

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

This is the json file that generate from a url, i try to decode it by using 这是从url生成的json文件,我尝试通过使用对其进行解码

$json = file_get_contents('url');
$obj = json_decode($json,true);

and i was able to get the first element 我能够获得第一个要素

echo $obj['success'];

However how to access the following part within the photos. 但是,如何访问照片中的以下部分。 I search a lot of example but all won't be able to work. 我搜索了很多示例,但都无法正常工作。 Anyone help me ? 有人帮我吗?

as you can see if you print_r($obj) : 如您所见,如果您print_r($obj)

  • "photos" is an array “照片”是一个数组
  • the field "tags" of the "photos" array is an array “照片”数组的字段“标签”是一个数组
  • "confirmed" is a field of the "tags"-array “确认”是“标签”数组的一个字段

So do it like this 所以这样做

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

the output is: 输出为:

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

to directly access it you should do: 要直接访问它,您应该执行以下操作:

// 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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM