使用Gson.fromJson()方法解析json到类< T>模型不起作用 [英] Parsing json by using Gson.fromJson() method into class< T > model is not working
问题描述
我的问题是,我无法使用Gson.fromJson()方法解析json到类< T> model。
在我的 PageOutput.java
中, public class PageOutput< T> {
@SerializedName(TotalItemCount)
public int totalItemCount;
@SerializedName(Items)
private T [] items;
public final T [] getItems(){
return items;
在 Property.java
public class Property {
@SerializedName(Id)
public String id;
@SerializedName(Title)
public String title;
@SerializedName(Price)
public BigDecimal price;
....
在 Main.java ,
PageOutput<属性> output = new Gson.fromJson(jsonString,new TypeToken< PageOutput< Property>>(){}。getType());
当编译项目时,我在Items中得到null。
但是,当我使用
PageOutput< Property> output = new Gson.fromJson(jsonString,PageOutput.class);
我将得到T []项的通用对象类型的结果,同时我应该得到属性类型。 (第18行)
output = {com.myapp.PageOutput@830048974608}
items = {java.lang。 Object [17] @ 830048616120} //第18行
[0] = {com.google.gson.internal.LinkedTreeMap@830048777632} size = 16
[1] = {com.google.gson。 internal.LinkedTreeMap$Node@830047149712}Id - > 2936918.0
[2] = {com.google.gson.internal.LinkedTreeMap$Node@830047452528}标题 - > 槟城Valdor
[3] = {com.google.gson.internal.LinkedTreeMap$Node@830047985896}价格 - > 7.5698E9
当我投射这个物件时,
属性[] properties = output.getItems(); //返回错误
我会收到错误:
无法转换为com.myapp.model.Property []
我该如何解决问题?请帮忙,非常感谢。
这是解析它的正确方法
PageOutput<属性> output = new Gson.fromJson(jsonString,new TypeToken< PageOutput< Property>>(){}。getType());
并且在项目中为null意味着您需要检查您的json字符串以及它是如何构建的 p>
My problem is, I can't parse json by using Gson.fromJson() method into class< T > model.
In my PageOutput.java
public class PageOutput<T> {
@SerializedName("TotalItemCount")
public int totalItemCount;
@SerializedName("Items")
private T[] items;
public final T[] getItems() {
return items;
}
}
In Property.java
public class Property {
@SerializedName("Id")
public String id;
@SerializedName("Title")
public String title;
@SerializedName("Price")
public BigDecimal price;
....
}
In Main.java,
PageOutput<Property> output = new Gson.fromJson(jsonString, new TypeToken<PageOutput<Property>>(){}.getType());
When project is compiled, I get null in Items.
But when I use
PageOutput<Property> output = new Gson.fromJson(jsonString, PageOutput.class);
I will get the result in generic object type for T[] items, by the meantime I should get Property type. (Line 18)
output = {com.myapp.PageOutput@830048974608}
items = {java.lang.Object[17]@830048616120} //Line 18
[0] = {com.google.gson.internal.LinkedTreeMap@830048777632} size = 16
[1] = {com.google.gson.internal.LinkedTreeMap$Node@830047149712}"Id" -> "2936918.0"
[2] = {com.google.gson.internal.LinkedTreeMap$Node@830047452528}"Title" -> "Valdor, Penang"
[3] = {com.google.gson.internal.LinkedTreeMap$Node@830047985896}"Price" -> "7.5698E9"
And when I cast this object,
Property[] properties = output.getItems(); //Return error
I will get error:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to com.myapp.model.Property[]
How can I solve the problem? Please help and thank you very much.
This is the right way to parse it
PageOutput<Property> output = new Gson.fromJson(jsonString, new TypeToken<PageOutput<Property>>(){}.getType());
and getting null at items means that you need to check your json string and how it was built
这篇关于使用Gson.fromJson()方法解析json到类< T>模型不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!