2014-02-22 50 views
1
{"coord":{"lon":73.69,"lat":17.8},"sys":{"message":0.109,"country":"IN","sunrise":1393032482,"sunset":1393074559},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":293.999,"temp_min":293.999,"temp_max":293.999,"pressure":962.38,"sea_level":1025.86,"grnd_level":962.38,"humidity":78},"wind":{"speed":1.15,"deg":275.503},"clouds":{"all":0},"dt":1393077388,"id":1264491,"name":"Mahabaleshwar","cod":200} 

我想从上面的json获取天气的描述,但在php中出现错误。我已经尝试了下面的PHP代码:在PHP中访问JSON值

$jsonDecode = json_decode($contents, true); 
$result=array(); 

foreach($jsonDecode as $data) 
{ 

    foreach($data{'weather'} as $data2) 
    { 

    echo $data2{'description'}; 
    } 

} 

任何帮助表示赞赏。我是使用json的新手。

+0

当你尝试时发生了什么?你有错误吗?如果是这样,哪一个?如果你只是想得到天气描述,你可以简单地做:'$ result = $ jsonDecode ['weather'] [0] ['description'];'。 [**查看演示**](https://eval.in/104555) –

+0

您只需调用一次json_deconde。 –

+0

我只打了一次json_decode。 – daman

回答

1

你必须用方括号([])访问数组元素,不卷曲的人({} )。

因此,你的代码应该改变,以反映这些变化:

foreach($data['weather'] as $data2) 
{ 
    echo $data2['description']; 
} 

而且,你的外foreach循环会导致你的代码做一些完全不同的比你想,你应该这样做:

foreach($jsonDecode['weather'] as $data2) 
{ 
    echo $data2['description']; 
} 
+0

尝试,但没有帮助。 – daman

+0

打开一个错误报告。二,显示'var_dump($ jsonDecode)'返回的内容。 – Mark

+0

删除额外的循环使其工作。 – daman

0

你要访问 “天气” 与 “[]” 操作符

这样,

$data["weather"] 
+0

一个小提示:他的代码明确地将'true'作为第二个值传递给'json_decode',这意味着'$ jsonDecode'的内容是一个关联数组,而不是一个对象。 http://ie1.php.net/json_decode –

+0

没有帮助。我得到这个:为foreach提供的无效参数() – daman

+0

@ sonu703删除外部的foreach,而不是$内部foreach使用$ jsonDecode –

0

如果解码的结果是一个数组,使用:

$data['weather'] 

如果结果是一个对象,使用方法:

$data->weather 
1

你$ jsonDecode似乎是一个数组,所以这应该与工作

foreach($jsonDecode['weather'] as $data) 
{ 
    echo $data['description']; 
} 
+0

工作。非常感谢。 – daman

1

您可以用示波器直接访问数据

$json = '{"coord":{"lon":73.69,"lat":17.8},"sys":{"message":0.109,"country":"IN","sunrise":1393032482,"sunset":1393074559},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":293.999,"temp_min":293.999,"temp_max":293.999,"pressure":962.38,"sea_level":1025.86,"grnd_level":962.38,"humidity":78},"wind":{"speed":1.15,"deg":275.503},"clouds":{"all":0},"dt":1393077388,"id":1264491,"name":"Mahabaleshwar","cod":200}'; 
$jsonDecode = json_decode($json, true); 

echo $jsonDecode['weather'][0]['description']; 
//output Sky is Clear 

正如你可以看到wheater`被范围包围着,这意味着它是另一个数组。您可以循环抛出一个数组,如果你有一个以上的结果

foreach($jsonDecode['weather'] as $weather) 
{ 
    echo $weather['description']; 
} 

Live demo

+0

谢谢。很好知道将来使用。 – daman

+0

天气是一个自己的数组,这意味着将会有更多的项目,而不仅仅是一个:) – Mark

+0

@RainFromHeaven这就是为什么我把这两种方式以及你没有的样本;) – Fabio

0

有几件事情值得回答你的问题更多:

问:json_decode($data)json_decode($data, true)之间的区别?
一个:前转换JSON对象到一个PHP对象,后者产生一个关联数组:http://uk1.php.net/json_decode

在任一情况下,存在于遍历结果没有意义。你可能想访问只是 '天气' 字段:

$o = json_decode($data) =>使用$weather = $o->weather
$a = json_decode($data, true) =>使用$weather = $a['weather']

一旦你的 '天气' 字段,仔细看它是什么: "weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}]

它是一个包含单个对象的数组。这意味着你需要遍历它,或者使用$clearSky = $weather[0]。在这种情况下,你选择了哪种json_decode方法并不重要=> JSON数组总是被解码为PHP(数字索引)数组。

但是,一旦您获得$clearSky,您正在访问该对象,并且它也很重要,您选择哪种方法 - 使用箭头或方括号,与第一步类似。

因此,要获得的exaple天气的描述正确的方法是,要么这些:

json_decode($data)->weather[0]->description
json_decode($data, true)['weather'][0]['description']

注意:在后一种情况下,函数调用的非关联化的结果,才支持在PHP 5.4或更新。在PHP 5.3或更高版本中,您必须创建一个变量。

注意:我还鼓励您始终使用isset检查预期字段是否实际设置在结果中。否则,您将尝试访问未定义的字段,这会引发错误。