1

私はPHPを初めて使用しますが、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. 次の2日間の天気情報

私はすべての情報が欲しいのではなく、特定の情報だけが欲しい

current_condition
       temp_C
       temp_F
       weatherDesc

次の2日間の天気情報からのデータが欲しいより

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

ただし、上記のように値をフェッチする方法がわからない場合は、リソースのヘルプやポインタが本当に役立ちます

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;

将来の2日間の配列を取得できます(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 に答える