简体   繁体   中英

Unable to parse JSON using GSON in playframework because of nested bean

I have created a Bean as a representation of a JSON object that I will be parsing. Problem is, the bean has nested classes. Because of which, compilation fails. I verified by removing the class. The app compiles properly. This is the error I get:

2012-11-05 19:23:30,783 ERROR ~ 

@6c8po9gbf
Internal Server Error (500) for request GET /favicon.ico

Oops: NullPointerException
An unexpected error occured caused by exception NullPointerException: null

play.exceptions.UnexpectedException: Unexpected Error
    at play.Play.start(Play.java:545)
    at play.Play.detectChanges(Play.java:629)
    at play.Invoker$Invocation.init(Invoker.java:198)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
    at play.classloading.ApplicationCompiler$2.acceptResult(ApplicationCompiler.java:266)
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:478)
    at play.classloading.ApplicationCompiler.compile(ApplicationCompiler.java:282)
    at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:424)
    at play.Play.start(Play.java:505)
    ... 3 more

The javabean class is

import java.util.List;

import com.google.gson.Gson;

public class GsGetChallenge {

    public static void main(String... args) throws Exception {

        String json = "{\"response\":\n" + "    {\"salt\":\n"
                + " \"zP9UFJOklQLePOqf0lSh0NgdlXWAt8qhIq4adcP1opdkz8UwVz\",\n"
                + " \"status\":\"SUCCESS\",\n"
                + " \"challenge\":\"BXuQQ2056310911\"\n" + "}}";

        ResponseData responsee = new Gson().fromJson(json, ResponseData.class);

        // Show it.
        System.out.println(responsee.getResponse().getChallenge());
    }

}

class ResponseData {

    private Response response;

    public static class Response {
        private String salt;
        private String status;
        private String challenge;

        public String getSalt() {
            return salt;
        }

        public void setSalt(String salt) {
            this.salt = salt;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getChallenge() {
            return challenge;
        }

        public void setChallenge(String challenge) {
            this.challenge = challenge;
        }
    }

    public Response getResponse() {
        return response;
    }
    public void setResponse(Response response) {
        this.response = response;
    }
}

What can I do to use GSON, in such a case? I am using play 1.2.4.

move ResponseData to another .java or move ResponseData into GsGetChallenge and change it to static class GsGetChallenge. like

public static void index() {
    String json = "{\"response\":\n" + "    {\"salt\":\n"
    ...
}

static class ResponseData {
    private Response response;
    ...

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