Jagdeep
Jagdeep

Reputation: 159

JSON to Java conversion and mapping with DAO

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.

Upvotes: 0

Views: 3644

Answers (2)

Marcinek
Marcinek

Reputation: 2117

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.

Upvotes: 1

Daniel
Daniel

Reputation: 6491

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

Upvotes: 1

Related Questions