What should be my class structure to parse JSON in GSON

梦想的初衷 提交于 2020-01-06 16:11:30

问题


I have the following JSON data:

{
    "response": {},
    "errorMessage": {
        "error": [
            {
                "errorId": 260003,
                "domain": "ads",
                "subdomain": "asd",
                "severity": "asd",
                "category": "asd",
                "message": "asdsa  asd ad",
                "errorName": "UnAuthorized"
            }
        ]
    }
}

Currently I have the following class structure:

public class JSONCollection
    private Response response;
    private ErrorMessage error;

public class Response 
    private String collectionId;
    private String url;

public class ErrorMessage 
    private List<ErrorValues> error;

public class ErrorValues 
    private String errorId;
    private String domain;
    private String subdomain;
    private String severity;
    private String category;
    private String message;
    private String errorName;

I have setters/get set for all private variables

But when I do a JSONCollection cJson = gson.fromJson(JSONValue,JSONCollection.class); I get cJson as a null.

How to get it right?


回答1:


I used this tool shown by @JigarJoshi to generate my schema.

The only difference I found is I had to change the class name from ErrorValues to Error



来源:https://stackoverflow.com/questions/17794916/what-should-be-my-class-structure-to-parse-json-in-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!