dongqiao8421 2016-12-02 23:39
浏览 30

Json_decode抛出“试图获取非对象的属性”

I'm trying to use the simple weather API, but somehow it doesn't recognize it as an object whatever I do. This is my code:

$url = "https://www.amdoren.com/api/weather.php?api_key=za8LEJ8F9mcHK8SvLxdM98rM9mNFjW&lat=40.7127837&lon=-74.0059413";
$curl = curl_init($url);
$curl_response = curl_exec($curl);
$jsonobj = json_decode($curl_response);
$msg = "Temperature in ".$city."will be: ". $jsonobj->forecast->max_c;

and this is the data I'm trying to reach with $jsonojb->forecast->max_c:

{
 "error" : 0,
 "error_message" : "-",
 "forecast":[ 
 {"date":"2016-12-02",
 "avg_c":8,
 "min_c":5,
 "max_c":11,
 "avg_f":46,
 "min_f":41,
 "max_f":52,
  (...)

but it doesn't work. What am I doing wrong guys?

  • 写回答

3条回答 默认 最新

  • doubu2730 2016-12-02 23:47
    关注

    forecast is an array, so you have to use like this:

    $forecast = $jsonobj->forecast;
    $forecast[0]->max_c;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大