doutou7740 2014-02-18 11:01
浏览 16
已采纳

json解码来自“{”和“[”[关闭]

Im trying to json_decode the following:

   "main":{
      "temp":9.04,
      "temp_min":9.04,
      "temp_max":9.04,
      "pressure":938.13,
      "sea_level":1037.57,
      "grnd_level":938.13,
      "humidity":87
   },
   "weather":[
      {
         "id":801,
         "main":"Clouds",
         "description":"few clouds",
         "icon":"02d"
      }
   ],


$result = json_decode($json);
$temp = $result->main->temp; //is displaying the data
however
$id = $result->weather->id; //is not displaying anything

all i can see is difference that te second one have an extra "[]"

can you help me telling how can i get weather->id from that json, thank you

  • 写回答

1条回答 默认 最新

  • dtvam48220 2014-02-18 11:05
    关注

    The weather element is an array: it contains a list of elements. To get what you want from that exact example you would want:

    $id = $result->weather[0]->id;
    

    You also might want to think about what you want to happen if there is more than one element in that array, or if there are zero.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?