简体   繁体   中英

GSON stackoverflow error

I'm trying to get the array of filters from a Json string using Gson, but im keep on getting this error, does anyone knows how can I fix this?

Here is logCat output:

 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)

Here is my JSON string:

{
    "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)"
        }
    ]
}

And here is my code:

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;
    }

}

and 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;
    }

}

There's coma missing on line 8 of your json. You can check your json here: http://www.jsoneditoronline.org/

According to the first response to this question: GSON troubles , Gson documentation suggest you to provide always a default contructor with no arguments for your classes. You can try coding such method in your Response and Filter, also you should check the BaseMo class.

此问题可能是由于xml布局文件内部的语法错误,文件命名错误或您在布局代码中某处引用的资源丢失而引起的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM