2016-07-15 2 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

나는 목록에서 그것을 원하는 개체가 아닙니다! –

관련 문제