JSON to Java conversion and mapping with DAO

痴心易碎 提交于 2019-12-08 11:54:27

问题


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.

Below is my 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
    }]
}

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]

Please help.

Thanks in advance.


回答1:


As the error suggests, the "release-1.0" field in your JSON is Unrecognized - meaning, you don't have a field with that name in your TestGroup class.

The JSON fileds must match the Class data members:

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

Would be a match for List<TestGroup> if TestGroup was:

@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.
}



回答2:


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>();

Create a class containing this field and jackson will bind a List of TestGroups to it.



来源:https://stackoverflow.com/questions/35000298/json-to-java-conversion-and-mapping-with-dao

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!