2

I’ve a simple question, and was wondering if someone here could help me how to parse through a Json string and extract all the User and put it into a List?

Note the object key directly under Users is a random number which can change with every response!

{
    "_links": {},
    "count": {},
    "users": {
        "123321": { //*Is a random number which can be any number
            "_links": {},
            "user": {
                "id": "123321",
                "name": "...",
                "age": "...",
                "address": ""
                ..
            }
        },
        "456654": {
            "_links": {},
            "user": {
                "id": "456654",
                "name": "...",
                "age": "...",
                "address": ""
                ...
            }
        }
        ...
    },
    "page": {}
}

Ideally, it would be nice to utilize Jackson Json or Gson.

Also I would like to only construct User object with two fields (id & name) and ignore the rest of the fields!

3
  • Create a class mapping the structure of the JSON (at least what you're interested into), and use Jackson to read an instance of that class. Use a Map<String, SomeObjectWithAUserFiel> for the users member. Commented Nov 12, 2017 at 22:48
  • How do I ignore the fields that I’m not interested? Can you provide and example of this class and how to use Jackson to iteration over the tree and create this ArrayList please! Commented Nov 12, 2017 at 22:51
  • By not putting them into the class. You need to start experimenting by yourself. Extracting the values from a map is a matter of reading the javadoc of Map. Commented Nov 12, 2017 at 22:54

2 Answers 2

3

A Jackson-approach could look like this:

ObjectMapper om = new ObjectMapper();
JsonNode node = om.readTree(input);

List<JsonNode> users = node.findValues("user");

From here you can either retrieve the values directly from the JsonNode instances, e.g.:

for (JsonNode jsonNode : users) {
    System.out.println(jsonNode.get("name"));
    System.out.println(jsonNode.get("id"));
}

or convert to a POJO:

for (JsonNode user : users) {
    User pojo = om.treeToValue(user, User.class);
    System.out.println(pojo.id);
    System.out.println(pojo.name);
}

Assuming the existence of a POJO that looks something like the following:

@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    public String name, id;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have to parse the json...which can be done like -

JSONObject json=new JSONObject(response);
JSONObject json2=json.getJSONObject("users");
Iterator keys = json2.keys();

while(keys.hasNext()) {
    String key = (String)keys.next();
    JSONObject json3=json2.getJSONObject(key);
    ArrayList<User> list=new ArrayList<>();

    JSONObject userjson=json3.getJSONObject("user");    
    User user = new User();
    String id=userjson.getString("id");
    String name=userjson.getString("name");
    user.setId(id);
    user.setName(name);
    list.add(user);      
}

Hope it helps.. :)

3 Comments

No, the solution is not taking into account the random numbers.. This is most standard way of performing json parsing as far as i know..For 100 users it won't take time..I have implemented this in many standard apps having 5000 users.
Check the code now..It will work most probably. I have parsed random number too by ignoring them. the list will be containing only the user object, i.e list of users as you want..And What else can be a more effective way to do this, since we have to save each user object in list..so somehow we will do so in the loop only..It won't take time..Using streams class will do the same thing and the complexity will be same.
mvnrepository.com/artifact/org.json/json Mark this correct if it works.. :)

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.