java - 数组的 gson 模型

标签 java json parsing gson

我有一个如下所示的 JSON 文件:

{
"count": 60,
"value": [{
            "changesetId": 60,
            "url": "http://...",
            "author": {
                "id": "...",
                "displayName": "*...",
                "uniqueName": "...",
                "url": "http://...*
                "imageUrl": "http://..."
            },
            "checkedInBy": {
                "id": "...",
                "displayName": "...",
                "uniqueName": "...",
                "url": "http://...",
                "imageUrl": "http://..."
            },
            "createdDate": "2016-11-08T22:05:11.17Z",
            "comment": "..."
        },

我在创建模型以使用 API Gson 时卡住了。我开始是这样的:

public class Changesets{
    int count;
    *TODO* // model for the JSON above.
}

模型或整个模型的开始将不胜感激。我将使用它来反序列化。

编辑:我试过了;

public class Changesets {
    int count;
    int changeset;
    String url;
    Changeset.Author author;
    Changeset.CheckedInBy checkedInBy;
    String createdDate;
    String comment;
}

我可以在哪里成功编写变更集模型。

最佳答案

如果您确实需要对各个 Java 类建模,则需要对 JSON 结构进行逆向工程。 在您的情况下,它将是这样的:

public class Changesets{
    int count;
    List<Change> value;
}

我会让你完成工作。

但是,如果您只需要一个特殊的 Java 对象来处理您只对非常具体的属性值感兴趣的复杂 JSON 对象,则可以使用我在此答案中建议的解决方案: Dynamic JSON structure to Java structure

关于java - 数组的 gson 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516384/

相关文章:

javascript - 将特定的 JSON 数组存储在变量中

linux - 在 Python3 中使用 HTMLParser 解析 HTML

java - 如何使用 JavaCC/JJTree 来存储 token ?

java - 用户和管理员 Java 项目

Java 音频可视化工具 - 如何捕获实时声音输出到扬声器?

java - JAX-WS客户端: JAXB required?

java - 将字符串数组转换为 double 组

c# - 如何使 flutter 应用程序的 http 获取请求到本地主机?

python - 将每个请求的 json 响应写入文件

python - Windows 上的 Pandas read_csv 错误