Parsing multiple fields of a JSON object and adding to a list using java 8 collections and streams

Viewed 1357

I make a call to a REST end point, and it returns a JSON object as a String. I then parse the String to an Object.

final Object parse = JSONValue.parse(body);

I check if the object is an instance of a JSONArray, and add selected elements of the object to a list.

 if (parse instanceof JSONArray) {
        ((JSONArray) parse).stream().collect(Collectors.toList()).forEach(o -> list.add((String) ((JSONObject) o).get("id")));
        ((JSONArray) parse).stream().collect(Collectors.toList()).forEach(o -> list.add((String) ((JSONObject) o).get("uri")));
    }

As of now I add all of the "id" fields first, and then add all of the "uri" fields. In my list I currently have 34 ids followed by 34 uris. I would like to have each id followed by its corresponding uri. Any help would be great.

2 Answers
Related