GSON 堆栈溢出错误
GSON stackoverflow error
我正在尝试使用 Gson 从 Json 字符串中获取 filters
的数组,但我一直收到此错误,有人知道我该如何解决这个问题吗?
这里是 logCat 输出:
Process: com.betterbilljsonparser, PID: 20491
java.lang.WhosebugError
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at ...
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:141)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access0(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access0(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
这是我的 JSON 字符串:
{
"value": 1,
"value": 2,
"other_object":{
"someval":"lorem",
"other_val":false
},
"filters": [
{
"items": [
{
"default": true,
"order": 0,
"val": [
0
],
"name": "Nope"
},
{
"default": false,
"name": "iPhone 5c",
"val": [
148
],
"order": 1
},
{
"default": false,
"name": "iPhone 5s",
"val": [
149
],
"order": 2
},
{
"default": false,
"name": "iPhone 6",
"val": [
147
],
"order": 3
},
{
"default": false,
"name": "iPhone 6 Plus",
"val": [
146
],
"order": 4
},
{
"default": false,
"name": "Galaxy S5",
"val": [
150
],
"order": 5
},
{
"default": false,
"name": "Galaxy S6",
"val": [
154
],
"order": 6
},
{
"default": false,
"name": "HTC One M9",
"val": [
155
],
"order": 7
},
{
"default": false,
"name": "Sony Xperia Z3",
"val": [
152
],
"order": 8
}
],
"type": "long",
"key": "handset.model",
"title": "Want a new phone with your contract?"
},
{
"items": [
{
"default": true,
"name": "3G",
"val": [],
"order": 0
},
{
"default": false,
"name": "4G",
"val": [
"4G"
],
"order": 1
}
],
"type": "str",
"key": "network_generation_name",
"title": "3G or 4G? (4G is faster)"
}
]
}
这是我的代码:
Gson gson = new Gson();
Response data = gson.fromJson(jsonString, Response.class);
Response.class
public class Response extends BaseMo {
@Expose
private List<Filter> filters = new ArrayList<Filter>();
/**
* @return The filters
*/
public List<Filter> getFilters() {
return filters;
}
/**
* @param filters The filters
*/
public void setFilters(List<Filter> filters) {
this.filters = filters;
}
}
Fiter.class
public class Filter extends BaseMo {
@Expose
private List<Item> items = new ArrayList<Item>();
@Expose
private String type;
@Expose
private String key;
@Expose
private String title;
/**
*
* @return
* The items
*/
public List<Item> getItems() {
return items;
}
/**
*
* @param items
* The items
*/
public void setItems(List<Item> items) {
this.items = items;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return
* The key
*/
public String getKey() {
return key;
}
/**
*
* @param key
* The key
*/
public void setKey(String key) {
this.key = key;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
}
和Item.class
public class Item extends BaseMo {
@SerializedName("default")
@Expose
private boolean _default;
@Expose
private String name;
@Expose
private List<String> val = new ArrayList<String>();
@Expose
private long order;
public Item() {
}
public Item(boolean _default, String name, List<String> val, long order) {
this._default = _default;
this.name = name;
this.val = val;
this.order = order;
}
/**
* @return The _default
*/
public boolean isDefault() {
return _default;
}
/**
* @param _default The default
*/
public void setDefault(boolean _default) {
this._default = _default;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name The name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The val
*/
public List<String> getVal() {
return val;
}
/**
* @param val The val
*/
public void setVal(List<String> val) {
this.val = val;
}
/**
* @return The order
*/
public long getOrder() {
return order;
}
/**
* @param order The order
*/
public void setOrder(long order) {
this.order = order;
}
}
您的 json 第 8 行缺少昏迷。您可以在此处查看您的 json:
http://www.jsoneditoronline.org/
根据对这个问题的第一个回答:GSON troubles,Gson 文档建议您始终为 classes 提供不带参数的默认构造函数。你可以尝试在你的响应和过滤器中编写这样的方法,你也应该检查 BaseMo class.
此问题可能是由于 xml 布局文件内部的语法错误、文件命名错误或布局代码中某处引用的资源缺失引起的。
我正在尝试使用 Gson 从 Json 字符串中获取 filters
的数组,但我一直收到此错误,有人知道我该如何解决这个问题吗?
这里是 logCat 输出:
Process: com.betterbilljsonparser, PID: 20491
java.lang.WhosebugError
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
at ...
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:141)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access0(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:122)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access0(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.<init>(ReflectiveTypeAdapterFactory.java:92)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:91)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:142)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:83)
at com.google.gson.Gson.getAdapter(Gson.java:359)
这是我的 JSON 字符串:
{
"value": 1,
"value": 2,
"other_object":{
"someval":"lorem",
"other_val":false
},
"filters": [
{
"items": [
{
"default": true,
"order": 0,
"val": [
0
],
"name": "Nope"
},
{
"default": false,
"name": "iPhone 5c",
"val": [
148
],
"order": 1
},
{
"default": false,
"name": "iPhone 5s",
"val": [
149
],
"order": 2
},
{
"default": false,
"name": "iPhone 6",
"val": [
147
],
"order": 3
},
{
"default": false,
"name": "iPhone 6 Plus",
"val": [
146
],
"order": 4
},
{
"default": false,
"name": "Galaxy S5",
"val": [
150
],
"order": 5
},
{
"default": false,
"name": "Galaxy S6",
"val": [
154
],
"order": 6
},
{
"default": false,
"name": "HTC One M9",
"val": [
155
],
"order": 7
},
{
"default": false,
"name": "Sony Xperia Z3",
"val": [
152
],
"order": 8
}
],
"type": "long",
"key": "handset.model",
"title": "Want a new phone with your contract?"
},
{
"items": [
{
"default": true,
"name": "3G",
"val": [],
"order": 0
},
{
"default": false,
"name": "4G",
"val": [
"4G"
],
"order": 1
}
],
"type": "str",
"key": "network_generation_name",
"title": "3G or 4G? (4G is faster)"
}
]
}
这是我的代码:
Gson gson = new Gson();
Response data = gson.fromJson(jsonString, Response.class);
Response.class
public class Response extends BaseMo {
@Expose
private List<Filter> filters = new ArrayList<Filter>();
/**
* @return The filters
*/
public List<Filter> getFilters() {
return filters;
}
/**
* @param filters The filters
*/
public void setFilters(List<Filter> filters) {
this.filters = filters;
}
}
Fiter.class
public class Filter extends BaseMo {
@Expose
private List<Item> items = new ArrayList<Item>();
@Expose
private String type;
@Expose
private String key;
@Expose
private String title;
/**
*
* @return
* The items
*/
public List<Item> getItems() {
return items;
}
/**
*
* @param items
* The items
*/
public void setItems(List<Item> items) {
this.items = items;
}
/**
*
* @return
* The type
*/
public String getType() {
return type;
}
/**
*
* @param type
* The type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return
* The key
*/
public String getKey() {
return key;
}
/**
*
* @param key
* The key
*/
public void setKey(String key) {
this.key = key;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
}
和Item.class
public class Item extends BaseMo {
@SerializedName("default")
@Expose
private boolean _default;
@Expose
private String name;
@Expose
private List<String> val = new ArrayList<String>();
@Expose
private long order;
public Item() {
}
public Item(boolean _default, String name, List<String> val, long order) {
this._default = _default;
this.name = name;
this.val = val;
this.order = order;
}
/**
* @return The _default
*/
public boolean isDefault() {
return _default;
}
/**
* @param _default The default
*/
public void setDefault(boolean _default) {
this._default = _default;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name The name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return The val
*/
public List<String> getVal() {
return val;
}
/**
* @param val The val
*/
public void setVal(List<String> val) {
this.val = val;
}
/**
* @return The order
*/
public long getOrder() {
return order;
}
/**
* @param order The order
*/
public void setOrder(long order) {
this.order = order;
}
}
您的 json 第 8 行缺少昏迷。您可以在此处查看您的 json: http://www.jsoneditoronline.org/
根据对这个问题的第一个回答:GSON troubles,Gson 文档建议您始终为 classes 提供不带参数的默认构造函数。你可以尝试在你的响应和过滤器中编写这样的方法,你也应该检查 BaseMo class.
此问题可能是由于 xml 布局文件内部的语法错误、文件命名错误或布局代码中某处引用的资源缺失引起的。