3

我创建了一个 Bean 作为我将要解析的 JSON 对象的表示。问题是,bean 有嵌套类。因此,编译失败。我通过删除课程进行了验证。该应用程序正确编译。这是我得到的错误:

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

javabean类是

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;
    }
}

在这种情况下,我该怎么做才能使用 GSON?我正在使用播放 1.2.4。

4

1 回答 1

0

将 ResponseData 移动到另一个 .java 或将 ResponseData 移动到 GsGetChallenge 并将其更改为静态类 GsGetChallenge。喜欢

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

static class ResponseData {
    private Response response;
    ...
于 2012-12-25T19:18:35.557 回答