[英]Parse JSON to java Class
I have this JSON and I'm trying to parse it Java classes using GSON. 我有这个JSON,我正在尝试使用GSON解析它的Java类。 Here is the JSON 这是JSON
resp = "{"isVisible":true,"image":{"preferenceOrder":["Rose","Lilly","Lotus"]}}";
my parse code for java is this. 我的解析代码是这个。
ImageOrderResult result = new Gson().fromJson(resp,ImageOrderResult.class);
and here is the class which i have defined 这是我定义的类
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;
}
Here i'm missing out the flowers array part. 在这里,我错过了花阵列部分。 Parser is not able to fetch the list of values. 解析器无法获取值列表。 How do I solve it? 我该如何解决?
The problem is that you have the type of Image defined, but your class is missing a reference variable to actually "store" it in. You need to define your class like this for it to be properly serialized: 问题是你已经定义了Image类型,但你的类缺少一个实际“存储”它的引用变量。你需要像这样定义你的类才能正确序列化:
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>();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.