java 使用jackson动态嵌套json字符串到java对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25130629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:24:53  来源:igfitidea点击:

Dynamic nested json string to java object using Hymanson

javajsonobjectnestedHymanson

提问by user3324733

i have trouble in parsing json

我在解析 json 时遇到问题

I have a nested json string, i need to convert to java object , the string look like this, i want to ask how to handle this nested dynamic json using Hymanson, how to decode dynamic nested json string

我有一个嵌套的json字符串,我需要转换为java对象,字符串看起来像这样,我想问一下如何使用Hymanson处理这个嵌套的动态json,如何解码动态嵌套的json字符串

{

"resultCode": "0",
"dataObject": [
    {
        "lastSyncDate": "20140101000000",
        "count": 2,
        "refType": "ADO",
        "update": [
            {
                "artist": "C",
                "albumTitle": "道",
                "productTitle": "道",
                "thumbnail": "http://w.com/mposter/album/m/VACL00020880A_m.jpg",
                "lastSyncDate": "20140425120159",
                "refId": "VACL00214522"
            },
            {
                "artist": "楊",
                "albumTitle": "學",
                "productTitle": "美",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20140324161831",
                "refId": "VACP00168673"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 8,
        "refType": "PAT",
        "update": [
            {
                "artist": "方",
                "thumbnail": "http://t.com/moov/images/profile/PAT/8/70/00021870_tn_1_s.jpg",
                "lastSyncDate": "20140201010203",
                "refId": "00021870"
            },
            {
                "artist": "楊",
                "lastSyncDate": "20140328120831",
                "refId": "00000125"
            },
            {
                "artist": "陳",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140328185030",
                "refId": "00017704"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 4,
        "refType": "PAB",
        "update": [
            {
                "artist": "陳",
                "albumTitle": "The Key",
                "thumbnail": "http:/m.jpg",
                "lastSyncDate": "20140603143528",
                "refId": "VAUN00031629A"
            },
            {
                "artist": "何",
                "albumTitle": "梁",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20140603143528",
                "refId": "VAEA00003170A"
            },
            {
                "artist": "何",
                "albumTitle": "艷",
                "thumbnail": "http://m.jpg",
                "lastSyncDate": "20110603151452",
                "refId": "VAEA00003179A"
            }
        ],
        "delete": [ ]
    },
    {
        "lastSyncDate": "20140101000000",
        "count": 4,
        "refType": "PP",
        "update": [
            {
                "chiName": "其",
                "engName": "Other",
                "lastSyncDate": "20140130010203",
                "chiAuthor": "",
                "engAuthor": "",
                "refId": "PP1000000003"
            },
            {
                "chiName": "演",
                "engName": "E演",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140126010758",
                "chiAuthor": "專",
                "engAuthor": "Recommended",
                "refId": "PP1000000040"
            },
            {
                "chiName": "日本派台歌",
                "engName": "Japan New Releases",
                "lastSyncDate": "20140126010758",
                "chiAuthor": "",
                "engAuthor": "",
                "refId": "PP1000000057"
            },
            {
                "chiName": "9",
                "engName": "9",
                "thumbnail": "http://s.jpg",
                "lastSyncDate": "20140126010203",
                "chiAuthor": "專",
                "engAuthor": "Recommended",
                "refId": "PP1000000048"
            }
        ],
        "delete": [ ]
    }
]

}

回答by ipinyol

You onlyneed to reproduce the structure of the jsonusing java class structure. For instance, for your case:

需要重现jsonusing java 类结构的结构。例如,对于您的情况:

    public class Result {
        String resultCode;
        List<DataObject> dataObjects;
        <GETTERS & SETTERS>
    }

    public class DataObject {
        String lastSyncDate;
        int count;
        String refType;
        List<Update> updates;
        List<Delete> deletes;
        <GETTERS & SETTERS>
    }

    public class Update {
        String artist;
        String albumTitle;
        String productTitle;
        String thumbnail;
        String lastSyncDate;
        String refId;
        <GETTERS & SETTERS>
    }

    public class Delete {
        String refId;
        <GETTERS & SETTERS>
    }

I assumed that Delete class only contains the refId. With this structure, you'll be able to map the json to an object of the class Result without any problems doing:

我假设 Delete 类只包含 refId。使用此结构,您将能够将 json 映射到 Result 类的对象,而不会出现任何问题:

    byte[] jsonData = <YOUR JSON>
    ObjectMapper objectMapper = new ObjectMapper();
    Result result = objectMapper.readValue(jsonData, Result.class);

回答by criszhao

Since the field of Update is not fixed, use map instead of the Update class.

由于Update的字段不固定,所以使用map代替Update类。

    public class Result {
        String resultCode;
        List<DataObject> dataObject;
        <GETTERS & SETTERS>
    }

    public class DataObject {
        String lastSyncDate;
        int count;
        String refType;
        List<Map<String, String>> update;
        List<String> delete;
        <GETTERS & SETTERS>
    }

Assume that update only contains String values.

假设更新只包含字符串值。

    String jsonData = "YOUR JSON";
    ObjectMapper objectMapper = new ObjectMapper();
    Result result = objectMapper.readValue(jsonData, Result.class);