【问题标题】:php and nested json: how can i access this element?php 和嵌套 json:我如何访问这个元素?
【发布时间】:2013-09-29 23:07:58
【问题描述】:

这是 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",
        "precipMM": "0.0",
        "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'};

谁能告诉我我做错了什么?谢谢!

【问题讨论】:

  • 如果您的错误报告已激活,您应该会看到Trying to get property of non-object ...

标签: php json


【解决方案1】:

json_decode()TRUE 作为第二个参数给你一个关联数组。但您目前正尝试将其作为对象访问。

尝试以下方法:

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

【讨论】:

    【解决方案2】:

    这不是您在 PHP 中访问数组的方式

    $array['index']="value";
    
    echo $array['index1']['index2']
    

    你的例子:

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

    【讨论】:

      【解决方案3】:

      您可以使用json将json结果检索到一个变量中,然后使用变量信息在js中显示。

      $.ajax({
          'type': 'GET',
          'url': 'abc.com,
          'dataType': 'json',
          success: function (data) {
      
          var response = data;
          // alert(response.data.current_condition)  //something like that
          // for (var i = 0; i < response.length; i++) { }
      
          }
      

      【讨论】:

        【解决方案4】:

        您可以使用如下的 foreach 循环条件:

        $url = file_get_contents("http://blahblahblahblah");
        $arr = json_decode($url,true);
        foreach($arr as $data)  {
           echo $data['current_condition'][0]['temp_F'];
           echo $data['request'][0]['query'];
           echo $data['weather'][0]['date'];
        }
        

        【讨论】:

          猜你喜欢
          • 2018-04-09
          • 2021-07-09
          • 2020-10-25
          • 2017-07-19
          • 2016-07-16
          • 1970-01-01
          • 1970-01-01
          • 2023-01-10
          • 1970-01-01
          相关资源
          最近更新 更多