我正在尝试从下面的 json 中提取 full_name 值。我已经能够使用此代码从“查询”部分中提取内容。
echo $data->query->params->granularity;
这会打印出neighborhood
。
但我无法回显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"
}
]
}
}
最佳答案
第一个问题 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"
}
]
}
}
关于php - 如何用php从json数组中提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465289/