在android中解析我的json以获取特定值

时间:2016-07-15 06:54:56

标签: android 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}

3 个答案:

答案 0 :(得分: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 :(得分: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();
        }

答案 2 :(得分:0)

Surya提出的代码很好,但请注意这些字段是否可选。 如果是这样,您可以使用optDouble而不是getDouble。

在我看来,你正在使用Openweathermap作为天气提供商,如果是这样,我建议你看一下my post所以你不必担心自己解析json而你会发现所有有关如何以json格式处理天气信息的信息。