使用DAO进行JSON到Java的转换和映射 [英] JSON to Java conversion and mapping with DAO
问题描述
我有以下JSON响应,想要转换为Java,然后将数据保存到数据库。
I have below JSON response and want convert into Java and then later saving the data to database.
我查看了各种工具,但无法提出正确的解决方案。
I looked at various tools but not able to come up with proper solution.
我做错了什么但却无法理解差距在哪里。
I am doing something wrong but not able to understand where is the gap.
以下是我的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以上。
And Here is my Java class mapping to above 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;
}
}
我使用过杰克逊,但收到如下错误:
I have used Jackson but getting error as below:
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表达式:testcases:[
指的是一个列表。
You need a surrounding container class to map the field "release-1.0" as a List. Because the json expression: "testcases": [
refers to a list.
// 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列表绑定到它。
Create a class containing this field and jackson will bind a List of TestGroups to it.
这篇关于使用DAO进行JSON到Java的转换和映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!