w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Parsing JSON Object inside Array in Jackson

Maybe not the cleanest solution, but I tested it and it's working, using an Iterator for listes:

 ObjectMapper mapper = new ObjectMapper();
 UserDetails userDetails = new UserDetails();
        try {             
            userDetails = mapper.readValue(fileReader, UserDetails.class);

            System.out.println(userDetails.getFirst_name());
            System.out.println(userDetails.getLast_name());

            List<User_user> users = new ArrayList<User_user>();
            JsonNode rootNode = mapper.readTree(fileReader);
            JsonNode usersNode = rootNode.path("user_user");
            Iterator<JsonNode> ite = usersNode.elements();
            while (ite.hasNext()) {
                JsonNode temp = ite.next();               
                User_user userF = mapper.treeToValue(temp,
User_user.class);

                JsonNode favNode = temp.path("fav_colors");
                List<String> fav_colors = new
ArrayList<String>();
                Iterator<JsonNode> iter = favNode.elements();

                while (iter.hasNext()) {
                    fav_colors.add(iter.next().asText());
                }
                userF.setFav_colors(fav_colors);
                users.add(userF);
            }
            userDetails.setUser_user(users);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < userDetails.getUser_user().size(); i++) {
            System.out.println(userDetails.getUser_user().get(i).getId());
        }

I have modified also a bit the UserDetails.class:

public class UserDetails {
    private String city;
    private String first_name;
    private String last_name;
    private List<User_user> user_user;

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getFirst_name() {
        return this.first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return this.last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public List<User_user> getUser_user() {
        return this.user_user;
    }

    public void setUser_user(List<User_user> user_user) {
        this.user_user = user_user;
    } 
}

And here is the User_user.class with favorites colors:

public class User_user {
private String domain;
private String id;
public List<String> fav_colors;

public List<String> getFav_colors() {
    return fav_colors;
}

public void setFav_colors(List<String> fav_colors2) {
    this.fav_colors = fav_colors2;
}

public String getDomain() {
    return this.domain;
}

public void setDomain(String domain) {
    this.domain = domain;
}

public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}
}

See this question also for array and lists: Jackson - Json to POJO With Multiple Entries





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.