gpt4 book ai didi

java - 由于嵌套 bean,无法在 playframework 中使用 GSON 解析 JSON

转载 作者:行者123 更新时间:2023-12-03 23:14:28 25 4
gpt4 key购买 nike

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

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?我正在使用 play 1.2.4。

最佳答案

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

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

static class ResponseData {
private Response response;
...

关于java - 由于嵌套 bean,无法在 playframework 中使用 GSON 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13233671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com