将JSON解析为java类
我有这个JSON,我正试图用GSON解析它的Java类。这里是JSON将JSON解析为java类,java,json,gson,Java,Json,Gson,我有这个JSON,我正试图用GSON解析它的Java类。这里是JSON resp = "{"isVisible":true,"image":{"preferenceOrder":["Rose","Lilly","Lotus"]}}"; 我的java解析代码如下 ImageOrderResult result = new Gson().fromJson(resp,ImageOrderResult.class); 这是我定义的类 public class ImageOrderResult {
resp = "{"isVisible":true,"image":{"preferenceOrder":["Rose","Lilly","Lotus"]}}";
我的java解析代码如下
ImageOrderResult result = new Gson().fromJson(resp,ImageOrderResult.class);
这是我定义的类
public class ImageOrderResult {
//Used for general Error Tracing
public String status = "";
public String message = "";
public String errorTrace = "";
public class Image{
@SerializedName("preferenceOrder")
public ArrayList<String> flowers= new ArrayList<String>();
}
@SerializedName("isVisible")
public boolean isVisible= false;
}
公共类ImageOrderResult{
//用于一般错误跟踪
公共字符串状态=”;
公共字符串消息=”;
公共字符串errorTrace=“”;
公众阶级形象{
@SerializedName(“优先顺序”)
public ArrayList flowers=new ArrayList();
}
@SerializedName(“isVisible”)
公共布尔值isVisible=false;
}
在这里,我错过了花阵列部分。分析器无法获取值列表。如何解决它?问题是您已经定义了图像的类型,但是您的类缺少一个实际“存储”图像的引用变量。 您需要像这样定义类,以使其正确序列化:
public class ImageOrderResult {
//Used for general Error Tracing
public String status = "";
public String message = "";
public String errorTrace = "";
@SerializedName("image")
public Image image = null;
@SerializedName("isVisible")
public boolean isVisible= false;
public class Image{
@SerializedName("preferenceOrder")
public ArrayList<String> flowers= new ArrayList<String>();
}
}
公共类ImageOrderResult{
//用于一般错误跟踪
公共字符串状态=”;
公共字符串消息=”;
公共字符串errorTrace=“”;
@序列化名称(“图像”)
公共图像图像=空;
@SerializedName(“isVisible”)
公共布尔值isVisible=false;
公众阶级形象{
@SerializedName(“优先顺序”)
public ArrayList flowers=new ArrayList();
}
}
谢谢。我一点也不明白。