简体   繁体   中英

Converting a specific JSON to Java Object

I am able to convert simple Json into java objects using Google Gson. The issue is I am not able to convert this specific type of response to a java object. I think this is due to the fact that I am not sure how to actually model the java classes for it. The following is the Json:

{
"List": {
    "Items": [
        {
            "comment": "comment",
            "create": "random",
            "ID": 21341234,
            "URL": "www.asdf.com"
        },
        {
            "comment": "casdf2",
            "create": "rafsdfom2",
            "ID": 1234,
            "Url": "www.asdfd.com"
        }
    ]
},
"related": {
    "Book": "ISBN",
    "ID": "id"
}}

I haven't worked with Gson specifically, but I have worked with JSON / Java conversion in Jersey and JAXB. I would say that the easiest way to translate the JSON text is to map every {..} to a Class and [..] to a List.

For example:

Class Data {
    HistoryListClass HistoryList;
    Relation related;
}

Class HistoryListClass {
    List<History> history;
}

Class History {
    String comment;
    String create;
    int ID;
    String ttURL;
}

Class Relation {
    String book;
    String ID;
}

See also: Converting JSON to Java

The "HistoryList" element in the JSON code should probably be written "historyList", and just looking at the code given, I suggest you change it to contatain the list of "History" objects directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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