简体   繁体   English

用于嵌套对象和数组的 JSON 映射器

[英]JSON mapper for Nested objects and arrays

I have a Json String from a webservice response.我有一个来自网络服务响应的 Json 字符串。

This JSON sting has nested objects and arrays.这个 JSON 字符串具有嵌套的对象和数组。 I have tried to map it with java objects using jackson and GSON but i am getting errors in both the cases.我曾尝试使用 jackson 和 GSON 将它与 java 对象进行映射,但是在这两种情况下我都遇到了错误。

This is my Json:这是我的 Json:

{"events": [{
    "Code": "4", 
    "eventDataSet": {
        "Bar":{"EvDesc":"WRAP_UP"},
        "Foo":{
            "AcssId":"**1234",
            "EvCSId":"12‌​34",
            "custId":"3501234","Recid":"bknz"
        }
    }
]}

I want to pull the values of Bar and Foo objects from this json.我想从这个 json 中提取 Bar 和 Foo 对象的值。

Please suggest how can i map such type of responses.请建议我如何映射此类响应。

Libraries such as Gson and Jackson shouldn't have any trouble de-serializing valid JSON Strings.GsonJackson这样的库在反序列化有效的JSON字符串时应该不会有任何问题。

Most probably the problem is with object types you're using to de-serialize your inputs.问题很可能出在您用来反序列化输入的对象类型上。

You can use this site to generate POJO from one of your JSON responses.您可以使用此站点从您的JSON响应之一生成 POJO。 Input your JSON and select JSON as Source Type.输入您的 JSON 并选择JSON作为源类型。

Then, with Gson :然后,与Gson

Gson gson = new GsonBuilder().create();
Person p = gson.fromJson(inputString, Example.class);

Your json seems to have some problem.您的 json 似乎有问题。 Here is modified json, and I am able to convert it to Object using jackson.这是修改后的 json,我可以使用 jackson 将其转换为 Object。

{
    "events": [{
        "code": "4",
        "eventDataSet": {
            "bar": {
                "evDesc": "WRAP_UP"
            },
            "foo": {
                "acssId": "**1234",
                "evCSId": "12??34",
                "custId": "3501234",
                "recid": "bknz"
            }
        }
    }]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM