如何打印json对象和数组值? [英] How can i print json objects and array values?

查看:302
本文介绍了如何打印json对象和数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在结果字符串中,我具有要解析的全部数据,我需要在值内打印当前条件.

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

这是我的json数组,在这里我需要cloudcover,值内的weatherDesc数组,我该如何打印这些值. 我在这里做的是

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++) {

        }

在for循环中,如何重复和打印值,任何人都可以帮助我,谢谢.

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

推荐答案

假设您使用的是org.json,我将对数组进行如下迭代:

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();
    }

}

这篇关于如何打印json对象和数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆