Java 访问多级 JSON 结构中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20080422/
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-12 22:57:40  来源:igfitidea点击:

Access to the values in a multi-level JSON-structure

javajsonjson-simple

提问by Mark Korzhov

There is my JSON-structure:

有我的 JSON 结构:

{
   "date":"19.11.2013",
   "parent":{
      "child1":[
         {
            "date":"2013-11-19",
            "time":"10:30",
         },
         {
            "date":"2013-11-19",
            "time":"12:20",
         }
      ],
      "child2":[
         {
            "date":"2013-11-19",
            "time":"10:30",
         },
         {
            "date":"2013-11-19",
            "time":"12:20",
         }
      ]
   }
}

And it's my code at the moment:

这是我目前的代码:

public class json {
    public static void main(String[] args) throws IOException, ParseException {

        URL urlData = new URL("http://path.to/json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                urlData.openConnection().getInputStream(), "utf-8"));
        String struct = reader.readLine();

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(struct);
        JSONObject lev1 = (JSONObject) obj;
        System.out.println(lev1.get("date"));
    }
}

I got a datevalue (19.11.2013), but I don't know how to get child's values of dateand time. I'm using json-simple library.

我得到了一个日期值(19.11.2013),但我不知道如何获取孩子的datetime值。我正在使用 json-simple 库。

采纳答案by kiruwka

Here is the idea :

这是想法:

JSONObject parent = (JSONObject) lev1.get("parent");
JSONArray child1 = (JSONArray) parent.get("child1"); // same for child2
for (Object elem : child1) {
    System.out.prinlnt("date =  " + ((JSONObject) elem).get("date"));
    System.out.prinlnt("time =  " + ((JSONObject) elem).get("time"));
}

Let me know if it does not compile, otherwise should work.

如果它不编译,请告诉我,否则应该可以工作。

回答by Jhanvi

Since, the names of the array are child1and child2(which means the names are different), inside the parent object , first you have to fetch these names, and then for each array, fetch date and time.

因为,数组的名称是child1child2(这意味着名称不同),在父对象内部,首先您必须获取这些名称,然后对于每个数组,获取日期和时间。

Here struct, is the jsonString:

这里struct,是 jsonString:

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(struct);
        JSONObject lev1 = (JSONObject) obj;
        Object jObj = lev1.get("parent");
        List keys = new ArrayList();
        if (jObj instanceof Map) {
            Map map = (Map) jObj;
            Set keySet = map.keySet();
            for (Object s : keySet) {
                JSONObject jsonObj = (JSONObject) jObj;
                JSONArray jarr = (JSONArray) jsonObj.get(s.toString());
                for (int i = 0; i < jarr.size(); i++) {
                    Object get = jarr.get(i);
                    JSONObject job = (JSONObject) get;
                    String date = job.get("date").toString();
                    String time = job.get("time").toString();
                    System.out.println("Date: " + date + " , Time: " + time);
                }
            }
        }