【问题标题】:JSON to Java conversion and mapping with DAOJSON 到 Java 的转换和使用 DAO 的映射
【发布时间】:2016-05-02 05:12:08
【问题描述】:

我有以下 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
    }]
}

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

@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]

请帮忙。

提前致谢。

【问题讨论】:

    标签: java json jackson gson dao


    【解决方案1】:

    正如错误所示,JSON 中的 "release-1.0" 字段是 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
        }]
    ]
    

    如果 TestGroup 是,则匹配 List&lt;TestGroup&gt;

    @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】:

      您需要一个周围的容器类来将字段“release-1.0”映射为一个列表。因为 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 会将一个 TestGroup 列表绑定到它。

      【讨论】:

      • 感谢我更改了 JSON,现在它看起来如下:[{"release": "release-1.0","testcases":[{"id":55,"resourceId":"126","allGraphs":null,"isChecked":true},{"id":56,"resourceId":"125","allGraphs":null,"isChecked":true},{"id":58,"resourceId":"140","allGraphs":null,"isChecked":true}]}]
      • 我更新了我的答案:现在你需要一个额外的字段。但它现在应该可以工作了。
      • 现在我遇到了异常:com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.example.model.TestGroup out of START_ARRAY token at [Source: [{"release": "release-1.0","testcases":[{"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: 1]
      • 这是我用于转换的代码:// Convert JSON string to Object ObjectMapper mapper = new ObjectMapper(); List&lt;TestGroup&gt; testGroup1 = mapper.readValue(request.body(), new TypeReference&lt;List&lt;TestGroup&gt;&gt;() {}); for(TestGroup user : testGroup1) { System.out.println(user.getRelease()); }
      猜你喜欢
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      • 2013-01-26
      • 2014-02-10
      • 2023-04-05
      相关资源
      最近更新 更多