在Jackson中解析Array中的JSON对象

时间:2013-09-04 12:50:08

标签: android json jackson

使用 Jackson JSON Processor 从JSON获取详细信息,我有以下内容

JSON字符串

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

代码

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. 从上面的代码中我无法从User_user获取ID。

  2. 如何解析和get fav_colors if my JSON had array again

3 个答案:

答案 0 :(得分:4)

我将 List<?> user_user; 更改为列表&lt; User_user&gt; user_user;在UserDetails中 正如Michał Ziober

所建议的那样

之后我能够轻松地检索所有结果,这里是代码。
如果没有那些 Sysout ,那就很难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");
        }
    }

作为参考,我添加了project in GitHub

答案 1 :(得分:1)

也许不是最干净的解决方案,但我测试了它并且它正在工作,使用Iterator进行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());
        }

我还修改了一下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;
    } 
}

这是带有收藏夹颜色的User_user.class:

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

对于数组和列表,请参阅此问题:Jackson - Json to POJO With Multiple Entries

答案 2 :(得分:0)

回答关于解析fav颜色的第二个问题。 我在这里使用json,因为我对杰克森并不熟悉。它仍然可能对你有所帮助。

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