加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php和嵌套的json:我如何访问这个元素?

发布时间:2020-12-13 18:18:42 所属栏目:PHP教程 来源:网络整理
导读:这是json文本: {"data": { "current_condition": [{ "cloudcover": "75","humidity": "63","observation_time": "03:41 PM","precipMM": "0.0","pressure": "1020","temp_C": "15","temp_F": "59","visibility": "16","weatherCode": "116","weatherDesc":
这是json文本:
{
"data": {
    "current_condition": [{
        "cloudcover": "75","humidity": "63","observation_time": "03:41 PM","precipMM": "0.0","pressure": "1020","temp_C": "15","temp_F": "59","visibility": "16","weatherCode": "116","weatherDesc": [{
            "value": "Partly Cloudy"
        }],"weatherIconUrl": [{
            "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
        }],"winddir16Point": "SSE","winddirDegree": "160","windspeedKmph": "7","windspeedMiles": "4"
    }],"request": [{
        "query": "Northville,United States Of America","type": "City"
    }],"weather": [{
        "date": "2013-09-24","tempMaxC": "20","tempMaxF": "67","tempMinC": "8","tempMinF": "47","weatherCode": "113","weatherDesc": [{
            "value": "Sunny"
        }],"weatherIconUrl": [{
            "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
        }],"winddir16Point": "ESE","winddirDegree": "111","winddirection": "ESE","windspeedKmph": "10","windspeedMiles": "6"
    }]
}

}

我试图回应’temp_F’并且它无法正常工作.我无法弄清楚我做错了什么.我到目前为止:

$url = file_get_contents("http://blahblahblahblah");
$arr = json_decode($url,true);

这就是失败的地方.我已经完成了var_dump,所以我知道数据存在.但是我尝试过的每次“回声”尝试都会导致“数组”显示在屏幕上.我尝试过以下几种变体:

echo $arr->{'data'}->{'current_condition[0]'}->{'temp_F'};

有人能告诉我我做错了什么吗?谢谢!

json_decode(),TRUE作为第二个参数,为您提供关联数组.但是您目前正在尝试将其作为对象访问.

请尝试以下方法:

echo $arr['data']['current_condition'][0]['temp_F'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读