带有嵌套值的JSON到POJO

时间:2018-05-31 09:18:55

标签: java android json retrofit2

所以我有这样的json回复:

"data": [
    {
        "id": 11881,
        "date": "2018-03-26T16:22:08",
        "date_gmt": "2018-03-26T14:22:08",
        "guid": {
            "rendered": "http://google.com"
        },
        "modified": "2018-03-26T16:22:08",
        "modified_gmt": "2018-03-26T14:22:08",
        "slug": "some text",
        "status": "some status",
        "type": "post",
        "link": "http://google.com",
        "title": {
            "rendered": "some title"
        }
}
]

如果我有这样的类,我如何解析对POJO类的响应:

public class Response{
    private List<Post> data;
}

public class Post{
    private String id;
    private String date;
    private String date_gmt;
    private String guid;
}

问题是它有嵌套值如guid和title我如何将它解析为一个POJO类,我不需要为每个嵌套值创建pojo?

4 个答案:

答案 0 :(得分:2)

您可以按照以下提法创建Pojo.class

public class pojo {


private Title title;
private String link;
private String type;
private String status;
private String slug;
private String modified_gmt;
private String modified;
private Guid guid;
private String date_gmt;
private String date;
private int id;

public Title getTitle() {
    return title;
}

public void setTitle(Title title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getSlug() {
    return slug;
}

public void setSlug(String slug) {
    this.slug = slug;
}

public String getModified_gmt() {
    return modified_gmt;
}

public void setModified_gmt(String modified_gmt) {
    this.modified_gmt = modified_gmt;
}

public String getModified() {
    return modified;
}

public void setModified(String modified) {
    this.modified = modified;
}

public Guid getGuid() {
    return guid;
}

public void setGuid(Guid guid) {
    this.guid = guid;
}

public String getDate_gmt() {
    return date_gmt;
}

public void setDate_gmt(String date_gmt) {
    this.date_gmt = date_gmt;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public int getId() {
    return id;
}

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

public static class Title {
    private String rendered;

    public String getRendered() {
        return rendered;
    }

    public void setRendered(String rendered) {
        this.rendered = rendered;
    }
}

public static class Guid {
    private String rendered;

    public String getRendered() {
        return rendered;
    }

    public void setRendered(String rendered) {
        this.rendered = rendered;
    }
}
 }

你可以解析列表

 try {
        JSONObject jsonObject = new JSONObject("YOUR_RESPOSE");
        JSONArray data = jsonObject.getJSONArray("data");
        Type listType = new TypeToken<ArrayList<pojo>>() {
        }.getType();
        ArrayList<pojo>  list =new Gson().fromJson(data.toString(), listType);

    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

使用构造函数

创建一个类
public class Post{

private String id;
private String date;
private String date_gmt;
private String guid;

public Post(String id, String date, String date_gmt, String guid) 
{
    this.id = id;
    this.date = date;
    this.date_gmt = date_gmt;
    this.guid = guid;
}

创建解析逻辑

JSONArray json = new JSONArray(response.body().string());

String id;
String date;
String date_gmt;
JSONObject guid;
String rendered;

JSONObject resultObject = json.getJSONObject(0);

id = resultObject.getString("id");
date = resultObject.getString("date");
date_gmt = resultObject.getString("date_gmt");
guid = resultObject.getJSONObject("guid");                                
rendered = guid.getString("rendered");

Post post = new Post(id, date, date_gmt, rendered);

答案 2 :(得分:0)

                    **use jackson to parse json to java**

                     <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
                           <version>2.7.4</version>
                    </dependency>

                **creat Post.java** 
        **By using map you don't need to create separate pojo classes**
                package parser;

                import java.util.Date;
                import java.util.Map;

                public class Post {
                    private int id;
                    private Date date;
                    private Date date_gmt;
                    private Map<String, String> guid;
                    private Map<String, String> title;
                    private Date modified;
                    private Date modified_gmt;
                    private String slug;
                    private String status;
                    private String type;
                    private String link;
                    /*Getter and Setter Methods*/
                    public int getId() {
                        return id;
                    }
                    public void setId(int id) {
                        this.id = id;
                    }
                    public String getType() {
                        return type;
                    }
                    public void setType(String type) {
                        this.type = type;
                    }
                    public String getLink() {
                        return link;
                    }
                    public void setLink(String link) {
                        this.link = link;
                    }

                    public String getSlug() {
                        return slug;
                    }
                    public void setSlug(String slug) {
                        this.slug = slug;
                    }
                    public String getStatus() {
                        return status;
                    }
                    public void setStatus(String status) {
                        this.status = status;
                    }

                    public Date getModified() {
                        return modified;
                    }
                    public void setModified(Date modified) {
                        this.modified = modified;
                    }
                    public Date getModified_gmt() {
                        return modified_gmt;
                    }
                    public void setModified_gmt(Date modified_gmt) {
                        this.modified_gmt = modified_gmt;
                    }
                    public Date getDate() {
                        return date;
                    }
                    public void setDate(Date date) {
                        this.date = date;
                    }
                    public Date getDate_gmt() {
                        return date_gmt;
                    }
                    public void setDate_gmt(Date date_gmt) {
                        this.date_gmt = date_gmt;
                    }

                    public Map<String, String> getGuid() {
                        return guid;
                    }
                    public void setGuid(Map<String, String> guid) {
                        this.guid = guid;
                    }
                    public Map<String, String> getTitle() {
                        return title;
                    }
                    public void setTitle(Map<String, String> title) {
                        this.title = title;
                    }
                    @Override
                    public String toString() {
                        StringBuilder sb = new StringBuilder();
                        sb.append("\n-----Post Information-----\n");
                        sb.append("Date Gmt: " + getDate_gmt() + "\n");
                        sb.append("Date: " + getDate() + "\n");
                        sb.append("Modified Gmt: " + getModified_gmt() + "\n");
                        sb.append("Status: " + getStatus() + "\n");
                        sb.append("ID: " + getId() + "\n");
                        sb.append("Guid: " + getGuid() + "\n");
                        sb.append("Title:" + getTitle() + "\n");
                        sb.append("Slug: " + getSlug() + "\n");
                        sb.append("Type: " + getType() + "\n");
                        sb.append("*****************************");
                        return sb.toString();
                    }
                }  

            Then create Main class and i am reading json data  from file
            My file Json data as input-:
{
        "id": 11881,
        "date": "2018-03-26T16:22:08",
        "date_gmt": "2018-03-26T14:22:08",
        "guid": {
            "rendered": "http://google.com"
        },
        "modified": "2018-03-26T16:22:08",
        "modified_gmt": "2018-03-26T14:22:08",
        "slug": "some text",
        "status": "some status",
        "type": "post",
        "link": "http://google.com",
        "title": {
            "rendered": "some title"
        }
}


            package parser;

            import java.io.File;
            import java.io.IOException;

            import com.fasterxml.jackson.core.JsonParseException;
            import com.fasterxml.jackson.databind.JsonMappingException;
            import com.fasterxml.jackson.databind.ObjectMapper;

            public class JsonParser {
            public static void main(String[] args) {
                ObjectMapper objectMapper=new ObjectMapper();
                 try {
                     Post emp = objectMapper.readValue(new File("Q://employee.json"), Post.class);
                    System.out.println(emp.toString());
                } catch (JsonParseException e) {
                    e.printStackTrace();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            }

    ---------------output--------------------------
    -----Post Information-----
    Date Gmt: Mon Mar 26 19:52:08 IST 2018
    Date: Mon Mar 26 21:52:08 IST 2018
    Modified Gmt: Mon Mar 26 19:52:08 IST 2018
    Status: some status
    ID: 11881
    Guid: {rendered=http://google.com}
    Title:{rendered=some title}
    Slug: some text
    Type: post
    *****************************

答案 3 :(得分:-1)

你必须使用通用的JsonObject和chage getter和setter 以下是杰克逊的一个例子

    @JsonProperty("id")
    private Long id;
    @JsonProperty("date")
    private String date;
    @JsonProperty("date_gmt")
    private String dateGmt;
    @JsonProperty("guid")
    private ObjectNode guid;
    @JsonProperty("modified")
    private String modified;
    @JsonProperty("modified_gmt")
    private String modifiedGmt;
    @JsonProperty("slug")
    private String slug;
    @JsonProperty("status")
    private String status;
    @JsonProperty("type")
    private String type;
    @JsonProperty("link")
    private String link;
    @JsonProperty("title")
    private ObjectNode title;

    @JsonProperty("id")
    public Long getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(Long id) {
        this.id = id;
    }
    @JsonProperty("guid")
    public String getGuid() {
        return guid.get("rendered").asText();
    }

    @JsonProperty("guid")
    public void setGuid(ObjectNode guid) {
        this.guid = guid;
    }

}