2016-07-15 4 views
0

これは私のJSONです、私は唯一の次の圧力、最大温度、最小温度、湿度、温度が欲しいです。どのように私はそれを解析し、次の値を取得します。特定の値を取得するためにアンドロイドで私のJSONを解析する

String finalJSON = buffer.toString(); 
       JSONObject parentObject = new JSONObject(finalJSON); 
       JSONArray parentArray = parentObject.getJSONArray("weather"); 
       List<WeatherModel> weathermodellist = new ArrayList<>(); 
       for (int i = 1; i < parentArray.length(); i++) { 
        JSONObject finalObject = parentArray.getJSONObject(i); 
        WeatherModel weatherModel = new WeatherModel(); 
        weatherModel.setTemp((float) finalObject.getDouble("temp")); 
        weatherModel.setHumidity((float) finalObject.getDouble("humidity")); 
        weatherModel.setTemp_max((float) finalObject.getDouble("temp_max")); 
        weatherModel.setTemp_min((float) finalObject.getDouble("temp_min")); 
        weatherModel.setPressure((float) finalObject.getDouble("pressure")); 
        weathermodellist.add(weatherModel); 
       } 
       return weathermodellist; 

これは私のJSONです:

{"coord":{"lon":77.22,"lat":28.67},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"base":"cmc stations","main":{"temp":300.3,"pressure":998,"humidity":88,"temp_min":299.82,"temp_max":301.15},"wind":{"speed":3.1,"deg":80},"clouds":{"all":90},"dt":1468499400,"sys":{"type":1,"id":7809,"message":0.0025,"country":"IN","sunrise":1468454578,"sunset":1468504257},"id":1273294,"name":"Delhi","cod":200} 
+4

まず、コードを書式設定して、私の目を傷つけています。二番目;あなたはこれを検索しましたか?これには数百万もの質問、記事、チュートリアルがあります:http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – 0xDEADC0DE

答えて

1

はこれを試してみてください -

String finalJSON = buffer.toString(); 
JSONObject parentObject = new JSONObject(finalJSON); 

JSONObject main = parentObject.getJSONObject("main"); 

double temp = main.getDouble("temp"); 
double pressure= main.getDouble("pressure"); 
double humidity = main.getDouble("humidity"); 
double temp_min = main.getDouble("temp_min"); 
double temp_max = main.getDouble("temp_max"); 

はそれに役立つことを願っています:)

1
 String jsonStr="{'coord':{'lon':77.22,'lat':28.67},'weather':[{'id':500,'main':'Rain','description':'light rain','icon':'10d'}],'base':'cmc stations','main':{'temp':300.3,'pressure':998,'humidity':88,'temp_min':299.82,'temp_max':301.15},'wind':{'speed':3.1,'deg':80},'clouds':{'all':90},'dt':1468499400,'sys':{'type':1,'id':7809,'message':0.0025,'country':'IN','sunrise':1468454578,'sunset':1468504257},'id':1273294,'name':'Delhi','cod':200}"; 

     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 
      JSONObject mainJson =jsonObj.getJSONObject("main"); 
      double tempVal=mainJson.getDouble("temp"); 
      double pressureVal=mainJson.getDouble("pressure"); 
      double humidityVal=mainJson.getDouble("humidity"); 
      double temp_minVal=mainJson.getDouble("temp_min"); 
      double temp_maxVal=mainJson.getDouble("temp_max"); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
0

スーリヤによって提案されたコードは結構ですが、これらのフィールドがオプションかどうかに注意してください。 もしそうなら、getDoubleの代わりにoptDoubleを使うことができます。

天気プロバイダーとしてOpenweathermapを使用しているようです。my postを見てみることをお勧めします.jsonを自分で解析することを心配する必要はなく、対処方法に関するすべての情報を見つけることができますjson形式の気象情報。

+0

私はそれをリストにしたいものではなく、直接オブジェクトにします! –