1

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. Any idea how should I do this in Java ?

2
  • 2
    Google for a JSON library Commented Aug 10, 2015 at 20:19
  • @SamTebbs33 When I try to get the class of the output, it shows me ArrayList Commented Aug 10, 2015 at 21:16

1 Answer 1

1

Lets say you have the following json object:

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

    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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I get the classname for this output it shows me ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.