Java 如何打印 json 对象和数组值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23733020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:56:00  来源:igfitidea点击:

How can i print json objects and array values?

javajson

提问by tajMahal

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

这是我的 json 数组,这里我需要 cloudcover,weatherDesc值中的数组,我如何打印这些值。这里我做的是

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.

在 for 循环内如何重复和打印值,有人可以帮助我,提前致谢。

采纳答案by Prior99

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

假设您使用的是 org.json,我将按如下方式遍历数组:

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

}

回答by Namikaze Badri

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

您使用的是简单的 json 吗?如果是,请尝试:

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