简体   繁体   English

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

[英]Convert JSON inside an array list to an ArrayList (Java)

My output looks like this : 我的输出看起来像这样:

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

I want to create an ArrayList and and add all names in it if the value is not null. 我想创建一个ArrayList并在其中添加所有名称(如果值不为null)。 Any idea how should I do this in Java ? 知道我应该如何用Java做到这一点吗?

Lets say you have the following json object: 假设您有以下json对象:

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

to get the different fields inside of the object, create a JSONParser object and use the get() method to get the value held in that field 要获取对象内部的不同字段,请创建一个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();
    }
}

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

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