如何用php从json数组中提取数据?

时间:2014-08-23 18:48:55

标签: php arrays json twitter

我正在尝试从下面的json中提取full_name值。我已经能够从"查询"中提取使用此代码的部分。

echo $data->query->params->granularity;

打印出neighbourhood

但我无法回应full_name。我猜这是因为我必须做一些与[]不同的事情,但我不知道该怎么办,而且不确定该做什么。

这些似乎都不起作用。

foreach ($data['places'] as $item) { ough!
echo $item->result->places->contained_within->attributes->full_name;
echo $item->result->places->full_name;
echo $item->result->full_name;
echo $item->full_name;  
}

非常感谢任何帮助。

{
  "query": {
    "params": {
      "accuracy": 0,
      "coordinates": {
        "coordinates": [
          -122.42284884,
          37.76893497
        ],
        "type": "Point"
      },
      "granularity": "neighborhood"
    },
    "type": "reverse_geocode"
  },
  "result": {
    "places": [
      {
        "attributes": {

        },
        "bounding_box": {
          "coordinates": [
            [
              [
                -122.42676492,
                37.75983003
              ],
              [
                -122.420736,
                37.75983003
              ],
              [
                -122.420736,
                37.77226299
              ],
              [
                -122.42676492,
                37.77226299
              ]
            ]
          ],
          "type": "Polygon"
        },
        "contained_within": [
          {
            "attributes": {

            },
            "bounding_box": {
              "coordinates": [
                [
                  [
                    -122.51368188,
                    37.70813196
                  ],
                  [
                    -122.35845384,
                    37.70813196
                  ],
                  [
                    -122.35845384,
                    37.83245301
                  ],
                  [
                    -122.51368188,
                    37.83245301
                  ]
                ]
              ],
              "type": "Polygon"
            },
            "country": "United States",
            "country_code": "US",
            "full_name": "San Francisco, CA",
            "id": "5a110d312052166f",
            "name": "San Francisco",
            "place_type": "city"
          }
        ],
        "country": "United States",
        "country_code": "US",
        "full_name": "Mission Dolores, San Francisco",
        "id": "cf7afb4ee6011bca",
        "name": "Mission Dolores",
        "place_type": "neighborhood"
      }
    ]
  }
}

5 个答案:

答案 0 :(得分:1)

第一个问题是json无效。问题很少。您可以在以下网站进行验证:http://jsonlint.com/

json有两个全名。所以下面的代码将有助于提取这两个全名。

$json = 'Assign the json here';
$json_array = json_decode($json);
echo $json_array->result->places[0]->contained_within[0]->full_name;
echo $json_array->result->places[0]->full_name;
  • 将有效的json分配给单引号(')中的变量$json,因为json有双引号。

此处提供的测试代码:http://sugunan.net/demo/json1.php

如果我们采用您的foreach示例,则需要进行如下修改。但这不是经过测试的答案。

foreach ($data['places'] as $item) {
echo $item->contained_within[0]->full_name;
echo $item->full_name;
}

以下是上述验证过的json。哪几个没有必要的“,”。它错过了几个括号。

{
"query": {
    "params": {
        "accuracy": 0,
        "coordinates": {
            "coordinates": [
                -122.42284884,
                37.76893497
            ],
            "type": "Point"
        },
        "granularity": "neighborhood"
    },
    "type": "reverse_geocode"
},
"result": {
    "places": [
        {
            "attributes": {},
            "bounding_box": {
                "coordinates": [
                    [
                        [
                            -122.42676492,
                            37.75983003
                        ],
                        [
                            -122.420736,
                            37.75983003
                        ],
                        [
                            -122.420736,
                            37.77226299
                        ],
                        [
                            -122.42676492,
                            37.77226299
                        ]
                    ]
                ],
                "type": "Polygon"
            },
            "contained_within": [
                {
                    "attributes": {},
                    "bounding_box": {
                        "coordinates": [
                            [
                                [
                                    -122.51368188,
                                    37.70813196
                                ],
                                [
                                    -122.35845384,
                                    37.70813196
                                ],
                                [
                                    -122.35845384,
                                    37.83245301
                                ],
                                [
                                    -122.51368188,
                                    37.83245301
                                ]
                            ]
                        ],
                        "type": "Polygon"
                    },
                    "country": "United States",
                    "country_code": "US",
                    "full_name": "San Francisco, CA",
                    "id": "5a110d312052166f",
                    "name": "San Francisco",
                    "place_type": "city"
                }
            ],
            "country": "United States",
            "country_code": "US",
            "full_name": "Mission Dolores, San Francisco",
            "id": "cf7afb4ee6011bca",
            "name": "Mission Dolores",
            "place_type": "neighborhood"
        }
    ]
}

}

答案 1 :(得分:0)

由于您正在循环来自"地点"已经full_name属性已经在$item->full_name

同样适用于contained_within结构,$item->contained_within->full_name应该有效。

答案 2 :(得分:0)

访问已解码的JSON数据结构中的项目的快速指南:

通过验证器/格式化程序运行json,以便查看结构。我使用this formatter,因为您可以打开和关闭节点以轻松查看结构。

执行json_decode($json_string)时,PHP的json解码器会创建对象和(非关联)数组。要访问对象中的数据,请使用语法$object->Attribute。要访问数组中的数据,请为第一个项目提供数组项目的索引:$array[0],为第二个项目提供$array[1],等等。

示例:

$data = json_decode($json);
echo "accuracy: " . $data->query->params->accuracy . "\n";
// output: "accuracy: 0"
echo "coord 1: " . $data->query->params->coordinates->coordinates[0] . "\n";
// output: "coord 1: -122.42284884"

foreach ($data->query->params->coordinates->coordinates as $c)
    echo "coord: " . $c . "\n"
// output: 
// coord: -122.42284884
// coord: 37.76893497

要在数据结构中更深入地获取项目,请将这些访问器链接在一起。例如,要获得full_name,您将使用以下内容:

echo "Full name: " . $data->result->places[0]->full_name . "\n";
// output: Full name: Mission Dolores, San Francisco

答案 3 :(得分:0)

在我修复了JSON(可能是复制粘贴错误)之后,我使用了以下方法访问了第一个(最低分支)full_name

$data = json_decode($json);
foreach($data->result->places as $item) {
  echo $item->name; // Mission Dolores, San Francisco
}

使用以下命名full_name的第二个元素:

$data = json_decode($json);
foreach($data->result->places as $item) {
  foreach($item->contained_within as $cont) {
    echo $cont->full_name; // San Francisco, CA
  }
}

解析JSON背后的理念是,某些元素对一个级别有一组命名元素,另一个元素有一些重复的元素集。在这种情况下,有一个名为result -> places的元素可以不止一个。在那里有一个名为contained_within的元素,它有更多自己的元素。正确的方法是使用for / foreach对其进行循环,但也可以直接对其进行处理:

$data = json_decode($json);
echo $data->result->places[0]->full_name; // Mission Dolores, San Francisco
echo $data->result->places[0]->contained_within[0]->full_name; // San Francisco, CA

答案 4 :(得分:0)

你需要首先解码你的json然后做一个foreach ...你可以试试这个

$json = json_decode("YOUR JSON HERE");
foreach ($json->result->places as $place) {
    echo $place->full_name;
}
相关问题