我正在尝试使用Gson从Json字符串中获取filters
数组,但是我继续收到此错误,是否有人知道如何解决此问题?
这是logCat输出:
Process: com.betterbilljsonparser, PID: 20491
java.lang.StackOverflowError
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.access$100(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<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.access$100(ReflectiveTypeAdapterFactory.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<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;
}
}
答案 0 :(得分:2)
你的json第8行有昏迷。你可以在这里查看你的json: http://www.jsoneditoronline.org/
答案 1 :(得分:0)
根据对此问题的第一个回答:GSON troubles,Gson文档建议您始终为类提供没有参数的默认构造函数。您可以尝试在Response和Filter中编写此类方法,也应该检查BaseMo类。