簡體   English   中英

由於嵌套bean,無法在playframework中使用GSON解析JSON

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

我創建了一個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。

將ResponseData移動到另一個.java或將ResponseData移動到GsGetChallenge並將其更改為靜態類GsGetChallenge。 喜歡

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

static class ResponseData {
    private Response response;
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM