2012-11-04 111 views
1

我是PHP新手,不太清楚如何使用PHP解析JSON。这是JSON我是来自第三方用PHP解析复杂的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. 天气信息

我不想要所有的信息,但只有特定的一个像

current_condition 
     temp_C 
     temp_F 
     weatherDesc 

比我想的一些数据,从气象信息提供了未来两日

  1. 日期
  2. tempMaxC
  3. tempMinC
  4. weatherIconUrl
  5. windspeedKmph

我试着此代码在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"; 
    } 

但不知道如何获取值描述above.Any帮助或指针资源将真正有帮助

回答

3

你为什么不只是使用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"]; 
+0

我第一次尝试与你提到的一个,但有些我怎么又惹来袭如何获取特定信息 –

+0

@UmeshAwasthi检查我的新的答案 –

+0

+1你让我的疑惑变得清晰起来,我对PHP语法的正确使用感到震惊:)。试试看看它是如何为我 –

1

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

$data = json_decode($weather); 

然后你可以很容易地得到你想要的东西是这样的:

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

或循环...

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; 
-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; 
}