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.

2
  • 1
    Why do fine "user_user" property in this way List<?>? Could you try with List<User_user>? Commented Sep 4, 2013 at 21:36
  • @MichałZiober Thanks for the quick correction, that was the link I was missing. Commented Sep 7, 2013 at 4:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Should I change List to ArrayList ?
I have added my answer, give a try. +1 for your effort.
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());
        }

1 Comment

Yep I already mentioned its not using jackson, but you can have a idea for parsing.

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.