0

In the Result String i have entire data for that i'm parsing ,i need to print Current Conditions inside values.

current_condition": [ {"cloudcover": "75", "humidity": "71", "observation_time": "06:55 AM", "precipMM": "0.6", "pressure": "1009", "temp_C": "32", "temp_F": "90", "visibility": "10", "weatherCode": "116", "weatherDesc": [ {"value": "Partly Cloudy" } ], "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], "winddir16Point": "S", "winddirDegree": "170", "windspeedKmph": "9", "windspeedMiles": "6" } ]

This is my json array ,here i need cloudcover ,weatherDescarrays inside values,how can i print those values. Here what i did is

JSONParser parser = new JSONParser();
        Object obj1 = parser.parse(Result);
        JSONObject jobj = (JSONObject) obj1;
        JSONObject dataResult = (JSONObject) jobj.get("data");
        JSONArray current_condition = (JSONArray) dataResult.get("current_condition");
        //out.println(current_condition);
        for (int i = 0; i < current_condition.size(); i++) {

        }

inside for loop how to repeat and print values ,could anybody help me,thanks in advance.

4
  • Your input is not valid JSON. Test it at jsonlint.com Commented May 19, 2014 at 8:28
  • { "data": { "current_condition": [ {"cloudcover": "75", "humidity": "71", "observation_time": "06:55 AM", "precipMM": "0.6", "pressure": "1009", "temp_C": "32", "temp_F": "90", "visibility": "10", "weatherCode": "116", "weatherDesc": [ {"value": "Partly Cloudy" } ], "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], "winddir16Point": "S", "winddirDegree": "170", "windspeedKmph": "9", "windspeedMiles": "6" } ]}} Commented May 19, 2014 at 8:29
  • Which library are you using to parse this data? Commented May 19, 2014 at 8:33
  • Please edit your question. Don't put JSON into a comment. Commented May 19, 2014 at 8:53

2 Answers 2

2

Assuming that you are using org.json, I would iterate over the array as follows:

public static void main(String[] args) {
    String json = "{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"75\", \"humidity\": \"71\", \"observation_time\": \"06:55 AM\", \"precipMM\": \"0.6\", \"pressure\": \"1009\", \"temp_C\": \"32\", \"temp_F\": \"90\", \"visibility\": \"10\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\\/\\/cdn.worldweatheronline.net\\/images\\/wsymbols01_png_64\\/wsymbol_0002_su‌​nny_intervals.png\" } ], \"winddir16Point\": \"S\", \"winddirDegree\": \"170\", \"windspeedKmph\": \"9\", \"windspeedMiles\": \"6\" } ]}}";
    try {
        JSONObject jObj = new JSONObject(json);
        JSONObject dataResult = jObj.getJSONObject("data");
        JSONArray jArr = (JSONArray) dataResult.getJSONArray("current_condition");
        for(int i = 0; i < jArr.length();i++) {
            JSONObject innerObj = jArr.getJSONObject(i);
            for(Iterator it = innerObj.keys(); it.hasNext(); ) {
                String key = (String)it.next();
                System.out.println(key + ":" + innerObj.get(key));
            }
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you using json simple?, if yes, then try :

for (JSONObject object : current_condition) {
    System.out.println("cloudcover : " + object.get("cloudcover"));
    System.out.println("humidity : " + object.get("humidity"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.