简体   繁体   English

Java获取嵌套的JSON对象/数组

[英]Java get nested JSON object/array

Code used:使用的代码:

jObj = new JSONObject(json);
newJSONString = jObj.getString("payload");
JArray = new JSONArray(newJSONString);

This is what JArray looks like:这是 JArray 的样子:

[{"06:30:00":{"color":"grey","time_color":"black"},"06:45:00":{"color":"grey","time_color":"black"}}] [{"06:30:00":{"color":"grey","time_color":"black"},"06:45:00":{"color":"grey","time_color":"黑色的”}}]

Now I want to loop through the received times and print their color, how to do this?现在我想遍历接收到的时间并打印它们的颜色,该怎么做?

What I've tried:我试过的:

for (int i = 0; i < JArray.length(); ++i) {
    JSONObject rec = null;
    try {
        rec = JArray.getJSONObject(i);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    android.util.Log.e("print row:", String.valueOf(rec));
}

This just gives me this output:这只是给了我这个输出:

{"06:30:00":{"color":"grey","time_color":"black"},"06:45:00":{"color":"grey","time_color":"black"}} {"06:30:00":{"color":"grey","time_color":"black"},"06:45:00":{"color":"grey","time_color":"black "}}

You are getting this output since your JSON array contains only one JSON object which is - {"06:30:00":{"color":"grey","time_color":"black"},"06:45:00":{"color":"grey","time_color":"black"}}你得到这个输出是因为你的 JSON 数组只包含一个 JSON 对象,它是 - {"06:30:00":{"color":"grey","time_color":"black"},"06:45:00 ":{"color":"grey","time_color":"black"}}

Before answering your question, I would recommend you to go through JSON syntax.在回答您的问题之前,我建议您先了解一下 JSON 语法。 It will help you understand your question and answer effectively.它将帮助您理解您的问题并有效地回答。

Coming back to your question, in order to get "color" field from your nested JSON:回到你的问题,为了从嵌套的 JSON 中获取“颜色”字段:

  1. Traverse through keys in your JSON object.遍历 JSON 对象中的键。 In your case these are - "06:30:00" , "06:45:00".在你的情况下,这些是 - "06:30:00" , "06:45:00"。 You can google out solution to traverse through keys in JSON object in java.您可以通过 google 搜索解决方案以遍历 Java 中 JSON 对象中的键。

  2. Get nested object associated with given key(time) - you can use getJSONObject() method provided by Json library for this.获取与给定键(时间)关联的嵌套对象 - 您可以为此使用 Json 库提供的 getJSONObject() 方法。

  3. Get "color" field from json object - you can use optString() or getString() methods provided by Json library for this- depending upon whether your string is mandatory or optional.从 json 对象获取“颜色”字段 - 您可以为此使用 Json 库提供的 optString() 或 getString() 方法 - 取决于您的字符串是必需的还是可选的。

Here is working solution in java for your problem:这是针对您的问题的 Java 解决方案:

public static void getColor(JSONObject payloadObject) {
    try {
        JSONArray keys = payloadObject.names();
        for (int i = 0; i < keys.length(); i++) {
            String key = keys.getString(i); // Here's your key
            JSONObject value = payloadObject.getJSONObject(key); // Here's your value - nested JSON object
            String color = value.getString("color");
            System.out.println(color);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Please note, it is considered that object you receive as payload is a JSON object.请注意,您收到的作为有效载荷的对象被认为是一个 JSON 对象。

Hope this helps.希望这可以帮助。

Thanks.谢谢。

Use Keys() method which return Iterator<String> so that it will be easy for iterating every nested JSON使用返回Iterator<String> Keys()方法,以便迭代每个嵌套的JSON

for (int i = 0; i < JArray.length(); ++i) {
try {
   JSONObject rec = JArray.getJSONObject(i);

   Iterator<String> keys = rec.keys();
    while(keys.hasNext()) {
      String key1 = keys.next();
      JSONObject nested = rec.getJSONObject(key1);   //{"color":"grey","time_color":"black"}

      //now again same procedure
         Iterator<String> nestedKeys = nested.keys();
           while(nestedKeys.hasNext())  {
               String key2 = nestedKeys.next();
              System.out.println("key"+"..."+key2+"..."+"value"+"..."+nested.getString(key2);
             }
      }

} catch (JSONException e) {
    e.printStackTrace();
   }
android.util.Log.e("print row:", String.valueOf(rec));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM