将数组列表中的JSON转换为ArrayList(Java)

时间:2015-08-10 20:18:49

标签: java json arraylist

我的输出如下:

{
  "IssueField1":{
                 "id":"customfield_10561", 
                 "name":"Bug Disclaimer", 
                 "type":null, 
                 "value":"<div style>...</div>"
                }, 

  "IssueField2":{
                 "id":"customfield_13850", 
                 "name":"ENV Work Type (DT)", 
                 "type":null, 
                 "value":null
                }, 
   .
   .
   .

  "IssueField9":{
                 "id":"timespent",
                 "name":"Time Spent", 
                 "type":"null", 
                 "value":"null"
                 }
}

我想创建一个ArrayList,如果值不为null,则在其中添加所有名称。知道我应该如何在Java中做到这一点?

1 个答案:

答案 0 :(得分:1)

假设您有以下json对象:

{ "name":"john doe", "age":100, "job":"scientist", "addresses":["address 1","address 2","address 3"] }

获取对象内的不同字段,创建一个JSONParser对象并使用get()方法获取该字段中保存的值

    try {
        FileReader reader = new FileReader("/path/to/file.json");
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
        String name = (String) jsonObject.get("name");
        System.out.println("The name is " + name);
        long age = (long) jsonObject.get("age");
        System.out.println("The age is: " + age);
        JSONArray lang = (JSONArray) jsonObject.get("addresses");
        for (int i = 0; i < lang.size(); i++) {
            System.out.println("Address " + (i + 1) + ": " + lang.get(i));
        }
    } catch (FileNotFoundException fileNotFound) {
        fileNotFound.printStackTrace();
    } catch (IOException io) {
        io.printStackTrace();
    } catch (NullPointerException npe) {
        npe.printStackTrace();
    } catch (org.json.simple.parser.ParseException e) {
        e.printStackTrace();
    }
}
相关问题