du8864 2015-04-29 20:56
浏览 107

用JSON从数组中提取数据

Why does querying this JSON in this way not work?

$weather_data

{
   "coord":{
      "lon":-0.13,
      "lat":51.51
   },
   "sys":{
      "message":1.7255,
      "country":"GB",
      "sunrise":1430282118,
      "sunset":1430335221
   },
   "weather":[
      {
         "id":800,
         "main":"Clear",
         "description":"Sky is Clear",
         "icon":"01n"
      }
   ],
   "base":"stations",
   "main":{
      "temp":280.154,
      "temp_min":280.154,
      "temp_max":280.154,
      "pressure":1018.05,
      "sea_level":1026.07,
      "grnd_level":1018.05,
      "humidity":63
   },
   "wind":{
      "speed":3.3,
      "deg":253.501
   },
   "clouds":{
      "all":0
   },
   "dt":1430337359,
   "id":2643743,
   "name":"London",
   "cod":200
}

query.php

$json = json_decode($weather_data, TRUE);
$user_temp = $json['weather'][1]['main'];

My desired result is to get a value of "Clear" from inside that JSON. Troublesome.

I'm new to this, I've done some searching around and I couldn't discover a solution, thanks in advance for the help!

  • 写回答

1条回答 默认 最新

  • doxn43207 2015-04-29 21:00
    关注

    Arrays are zero based, you're trying to get the first item, and it would be [0]

    $json = json_decode($weather_data, TRUE);
    $user_temp = $json['weather'][0]['main'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line