1

我是 PHP 新手,不太清楚如何用 PHP 解析 JSON。这是我从第三方获得的 JSON

{ "data": 
  { "current_condition": 
   [ 
      {"cloudcover": "0", "humidity": "13", "observation_time": "05:47 AM", "precipMM": "0.0", 
      "pressure": "1016", "temp_C": "20", "temp_F": "69", 
      "visibility": "10", "weatherCode": "113",  
      "weatherDesc": [ {"value": "Sunny" } ],  
      "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
     " winddir16Point": "SW", "winddirDegree": "218", "windspeedKmph": "12", "windspeedMiles": "7" 
     } 
   ],
   "request": [ 
            {"query": "Lat 32.12 and Lon 76.53", "type": "LatLon" } 
      ],  
   "weather": [ 
          {
            "date": "2012-11-04", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "69", "tempMinC": "1", "tempMinF": "34", 
            "weatherCode": "113",  "weatherDesc": [ {"value": "Sunny" } ],  
            "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
            "winddir16Point": "SW", "winddirDegree": "218", "winddirection": "SW", "windspeedKmph": "12", "windspeedMiles": "8" 
           }, 
          {
           "date": "2012-11-05", "precipMM": "0.0", "tempMaxC": "20", "tempMaxF": "67", "tempMinC": "4", "tempMinF": "39", 
           "weatherCode": "113", "weatherDesc": [ {"value": "Sunny" } ], 
           "weatherIconUrl": [ {"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png" } ], 
           "winddir16Point": "SSW", "winddirDegree": "210", "winddirection": "SSW", "windspeedKmph": "12", "windspeedMiles": "7" 
           } 
          ] 
        }
     }

我将这些天气信息作为 JSON 数据获取,其中包括以下信息

  1. 当前信息
  2. 未来 2 天的天气信息

我不想要所有信息,而只想要特定的信息

current_condition
       temp_C
       temp_F
       weatherDesc

比我想要一些来自天气信息提供的未来 2 天的数据

  1. 日期
  2. 最高温度
  3. 温度MinC
  4. 天气图标网址
  5. 风速Kmph

我在 PHP 中尝试了这段代码

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($weather, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

这似乎给了我数组格式的 JSON 解码数据,但我对如何从 PHP 数据中获取这些特定值感到困惑。我可以遍历数据

foreach ($jsonIterator as $key => $value) {
    if(is_array($value)) {
        foreach ($value as $key => $value) {
    }

    } else {
       // echo "$key\n";
    }

但不确定如何如上所述获取值。资源的任何帮助或指针都会很有帮助

4

4 回答 4

4

为什么不直接使用json_decode然后处理生成的对象?

示例:http ://codepad.org/ciw3ogAu

我使用ob_get_content()是因为我不想弄乱转义序列,但重点在于这一行:

$result = json_decode($my_json_string);

获取信息并不难。例如,如果您想要以摄氏度为单位的当前温度:

echo $result->data->current_condition[0]->temp_C;

您可以获得未来两天的数组(http://codepad.org/bhOcd3eT):

$weatherarray = $result->data->weather; // An array with two elements

您使用$result->xxx而不是$result["xxx"]因为json_decode将为对象创建对象。您可以通过调用将其更改为数组json_decode($my_json_string, true),然后使用以下方式访问成员:

echo $result["data"]["current_condition"][0]["temp_C"];
于 2012-11-04T06:52:12.543 回答
2

您需要解码您的 json 对象,但不需要对其进行迭代。只需访问您想要的信息:

$decoded = json_decode($weather);
$date = $data->current_condition->data->weather->date;
$tempMaxC = $data->current_condition->data->weather->tempMaxC;
$tempMinC = $data->current_condition->data->weather->tempMinC;
$weatherUrl = $data->current_condition->data->weather->weatherIconUrl;
$windspeedKmph = $data->current_condition->data->weather->windspeedKmph;
于 2012-11-04T07:02:16.487 回答
1

我会采用这种方法来访问您的数据:

$data = json_decode($weather);

然后您可以通过这种方式轻松获得您想要的东西:

print_r($data->data->current_condition);

或循环...

于 2012-11-04T06:54:05.210 回答
-1
$decode=json_decode($file);    
echo $decode->data->current_condition[0]->temp_C; 
echo $decode->data->current_condition[0]->temp_F; 
echo $decode->data->current_condition[0]->weatherDesc[0]->value;   

foreach ($decode->data->weather as $data) {
  echo $data->date;
  echo $data->tempMaxC;
  echo $data->tempMinC;   
  echo $data->weatherIconUrl;
  echo $data->windspeedKmph;  
}
于 2013-11-22T09:39:33.380 回答