1

Am using Jackson JSON Processor to get details form JSON, I have the following

JSON String

{
"first_name": "first_name",
"last_name": "last",
"city": "Somewhere",
"user_user": [
   {
        "id": "1",
        "domain": "http://google.com/"
    },
    {
        "id": "34",
        "domain": "http://so.com/"
    },
    {
        "id": "42",
        "domain": "http://ww.com/"
    }
]}

UserDetails

package com.example.com;

import java.util.List;

public class UserDetails{
private String city;
private String first_name;
private String last_name;
private List<?> 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<?> getUser_user(){
    return this.user_user;
}
public void setUser_user(List<?> user_user){
    this.user_user = user_user;
}
}

User_user

package com.example.com;

import java.util.List;

public class User_user{
private String domain;
private String id;

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;
}
}

Code

ObjectMapper mapper = new ObjectMapper();
UserDetails userDetails = mapper.readValue(jsonString , UserDetails.class);

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

User_user userF =   mapper.readValue(jsonString , User_user.class);

for(int i = 0; i < userDetails.getUser_user().size(); i++)
{
System.out.println(userF.getId());
}
  1. From the above code am not able to get Id from User_user.

  2. How do I parse and get fav_colors if my JSON had array again.

VenomVendor
  • 15,064
  • 13
  • 65
  • 96

3 Answers3

4

I changed List<?> user_user; to List<User_user> user_user; in UserDetails
as suggested by Michał Ziober

After that I was able to retrieve all results easily, here is the code.
Without those Sysout it's hardly 3 lines.

    System.out.println(userDetails.getCity());      //City, FirstName, LastName 
    for(int i = 0; i < userDetails.getUser_user().size(); i++) {
    System.out.println(userDetails.getUser_user().get(i).getId());      // id, domain
    try {
        for(int k = 0; k < userDetails.getUser_user().get(i).getFav_colors().size(); k++) {
            System.out.println(userDetails.getUser_user().get(i).getFav_colors().get(k));   //fav_color
        }
    }
    catch (Exception e) {
        System.out.println("NO FAV COLORS");
        }
    }

For a reference I have added the project in GitHub.

Community
  • 1
  • 1
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
1

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

Community
  • 1
  • 1
Adinia
  • 3,722
  • 5
  • 40
  • 58
0

Answer for ur 2nd ques regarding the parsing the fav_color. I used json here, because am not much familiar with jackson. Still it may helpful for you.

String[] color = null;
        try {
            JSONObject obj = new JSONObject("urJsonData");          
            JSONArray jsonUser = obj.getJSONArray("user_user");

            for(int i = 0; i < jsonUser.length(); i++){
                JSONObject item = jsonUser.getJSONObject(i);
                if(item.toString().contains("fav_colors")){
                    JSONArray favColors = item.getJSONArray("fav_colors");
                    color = new String[favColors.length()];
                    for(int j =0; j < favColors.length(); j++){
                        color[j] = favColors.getString(j);
                    }
                }
                            //set ur fav_color color to ur data structure/object 

            }

        } catch (JSONException e) {
            Log.e("ERROR", e.toString());
        }
surender8388
  • 474
  • 3
  • 12