我有以下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;
}
}
我使用了杰克逊,但收到如下错误:
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]
请帮忙。
提前致谢。
答案 0 :(得分:1)
您需要一个周围的容器类来将字段“release-1.0”映射为List。因为json表达式:"testcases": [
引用了一个列表。
// will map to the new field release by name
private String release;
// Or mapped by named property
@JsonProperty("testcases")
private List<TestGroup> release10 = new ArrayList<TestGroup>();
创建一个包含该字段的类,jackson将一个TestGroups列表绑定到它。
答案 1 :(得分:1)
如错误所示,"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.
}
:
{{1}}