Search code examples
javajsonparsinggson

What should be my class structure to parse JSON in GSON


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?


Solution

  • 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