java - 如何将这个 JSON 响应转换为 POJO?

标签 java json gson pojo jsonschema2pojo

使用此 JSON,自动化 JSON 到 POJO 会严重失败。

请注意,一项请求的项目数量与另一项请求的项目数量不同。 在这里,我包含了包含 2 个项目的 JSON 响应。

{
    "status": 1,
    "complete": 1,
    "list": {
        "734233858": {
            "item_id": "734233858",
            "resolved_id": "734233858",
            "given_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1466459879",
            "time_updated": "1466459862",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 1,
            "resolved_title": "Developing Single Page Web Applications using Java 8, Spark, MongoDB, and AngularJS",
            "resolved_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/",
            "excerpt": "In this post you will learn how to use a micro framework called Spark to build a RESTful backend. The RESTful backend is consumed by a single page web application using AngularJS and MongoDB for data storage. I’ll also show you how to run Java 8 on OpenShift.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "2727"
        },
        "1015284226": {
            "item_id": "1015284226",
            "resolved_id": "1015284226",
            "given_url": "https://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1466458750",
            "time_updated": "1466458737",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 0,
            "resolved_title": "Spark Video Tutorials",
            "resolved_url": "http://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html",
            "excerpt": "Our friends over at learnhowtoprogram.com have been working on a series of Java courses for beginners, all of which feature Spark. This post contains an overview of these courses with direct links to their videos.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "41"
        }
    },
    "error": null,
    "search_meta": {
        "search_type": "normal"
    },
    "since": 1509309762
}

这个 JSON 对象的 POJO 会是什么样子?

最佳答案

你无法解析出list因为数字是随机的,所以对象相当好。您需要将其制作为 map 。否则,其余数据可由 Gson 解析。

class Foo {
    int status;
    int complete;
    TreeMap<String, Object> list;
    Object error;
    SearchMeta search_meta;
    long since;
}

class SearchMeta {
    String search_type;
}

您可以替换 <String, Object> map <String, InnerObject> ,其中InnerObject是该对象的 POJO

{
    "item_id": "734233858",
    "resolved_id": "734233858",
    "given_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/",
    "given_title": "",
    "favorite": "0",
    ...

关于java - 如何将这个 JSON 响应转换为 POJO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188934/

相关文章:

java - 类型 Dynamic Web Module 3.0 需要 Java 1.6 或更新版本,java 插件错误

postman 没有得到 Json View

php - Laravel 的 Response 内容必须是字符串或对象实现 __toString(), "object"given

android - GSON 不会将 Json 字符串转换为类

java - 如何使用 Gson 将嵌套 json 转换为 Map 而不进行类型转换

java - 如何分别从 HashMap 获取对象?

java - eclipse 中的 HttpServlet 错误

java - com.google.zxing.NotFoundException 核心java程序执行时出现异常?

javascript - 解决 "No ' Access-Control-Allow-Origin' header is present"问题与 Chrome 扩展

java.sql.Time 异常