gpt4 book ai didi

java - JSON 到 Java 的转换以及使用 DAO 的映射

转载 作者:行者123 更新时间:2023-12-02 04:00:18 25 4
gpt4 key购买 nike

我有以下 JSON 响应,想要转换为 Java,然后将数据保存到数据库。

我查看了各种工具,但无法找到合适的解决方案。

我做错了什么,但无法理解差距在哪里。

下面是我的 JSON:

{
"release-1.0": [{
"id": 55,
"resourceId": "126",
"allGraphs": null,
"isChecked": true
}, {
"id": 56,
"resourceId": "125",
"allGraphs": null,
"isChecked": true
}, {
"id": 58,
"resourceId": "140",
"allGraphs": null,
"isChecked": true
}]
}

这是我的 Java 类映射到上面的 JSON。

@DatabaseTable(tableName = "test_group")
public class TestGroup {
private List<TestGroup> testGroup;

public TestGroup() {
// ORMLite needs a no-arg constructor
}

@DatabaseField
private List<String> test_group_id;

@DatabaseField
private String id;

@DatabaseField
private String test_details;

@DatabaseField
private String graph_id;

public void setTestGroupID(List<String> testGroupId) {
this.test_group_id = testGroupId;
}

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

public void testDetails(String testDetails) {
this.test_details = testDetails;
}

public void setGraphId(String allGraphs) {
this.graph_id = allGraphs;
}

public List<TestGroup> getAllGraphs() {
return testGroup;
}
}

我使用了 Jackson,但出现如下错误:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "release-1.0" (class org.example.model.TestGroup), not marked as ignorable (4 known properties: "testGroupID", "graphId", "id", "allGraphs"])
at [Source: {"release-1.0":[{"id":55,"resourceId":"126","allGraphs":null,"isChecked":true},{"id":56,"resourceId":"125","allGraphs":null,"isChecked":true},{"id":58,"resourceId":"140","allGraphs":null,"isChecked":true}]}; line: 1, column: 17]

请帮忙。

提前致谢。

最佳答案

正如错误所示,"release-1.0"您的 JSON 中的字段是 Unrecognized - 意思是,您的 TestGroup 中没有具有该名称的字段类。

JSON字段必须与 Class 匹配数据成员:

[
{
"id": 55,
"resourceId": "126",
"allGraphs": null,
"isChecked": true
}, {
"id": 56,
"resourceId": "125",
"allGraphs": null,
"isChecked": true
}, {
"id": 58,
"resourceId": "140",
"allGraphs": null,
"isChecked": true
}]
]

将与 List<TestGroup> 匹配如果TestGroup是:

@DatabaseTable(tableName = "test_group")
public class TestGroup {

public TestGroup() {
// ORMLite needs a no-arg constructor
}

@DatabaseField
private String id;

@DatabaseField
private String resourceId;

@DatabaseField
private String allGraphs;

@DatabaseField
private Bollean isChecked;

// Getters and setters - preferably auto-generated since NAMES MUST MATCH.
}

关于java - JSON 到 Java 的转换以及使用 DAO 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35000298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com