嵌套的JSON随机名称对象使用Gson进行解析

时间:2018-01-31 19:36:33

标签: java android json gson

我正在寻找一种简单的方法将这个JSON解析为Gson java类,以便轻松地将他放入适配器并填充一些列表视图,问题是项目的一部分,我不知道项目对象名称,因为你可以看到我在项目中有随机对象名称,可以用GSON做到这一点,或者我需要另一个代码逻辑,这是针对Android的,谢谢你的帮助:

[{
    "id": 1001,
    "name": "Super1",
    "user": {
        "name": "The Super 1"
    },
    "items": {
        "987987M7812b163eryrt": {
            "id": 1,
            "strong": 456,
            "active": true,
            "sell": "te"
        },
        "90812bn120893juuh": {
            "id": 2,
            "strong": 4700,
            "active": true,
            "sell": "tt"
        },
        "981273jn19203nj123rg": {
            "id": 3,
            "strong": 3000,
            "active": true,
            "sell": "ti"
        }
    }
}]

将GSON格式的代码添加到JAVA Class:

import com.google.gson.annotations.SerializedName;
public class test_de {

/**
 * id : 1001
 * name : Super1
 * user : {"name":"The Super 1"}
 * items : {"987987M7812b163eryrt":{"id":1,"strong":456,"active":true,"sell":"te"},"90812bn120893juuh":{"id":2,"strong":4700,"active":true,"sell":"tt"},"981273jn19203nj123rg":{"id":3,"strong":3000,"active":true,"sell":"ti"}}
 */

private int id;
private String name;
private UserBean user;
private ItemsBean items;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public UserBean getUser() {
    return user;
}

public void setUser(UserBean user) {
    this.user = user;
}

public ItemsBean getItems() {
    return items;
}

public void setItems(ItemsBean items) {
    this.items = items;
}

public static class UserBean {
    /**
     * name : The Super 1
     */

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static class ItemsBean {
    /**
     * 987987M7812b163eryrt : {"id":1,"strong":456,"active":true,"sell":"te"}
     * 90812bn120893juuh : {"id":2,"strong":4700,"active":true,"sell":"tt"}
     * 981273jn19203nj123rg : {"id":3,"strong":3000,"active":true,"sell":"ti"}
     */

    @SerializedName("987987M7812b163eryrt")
    private _$987987M7812b163eryrtBean _$987987M7812b163eryrt;
    @SerializedName("90812bn120893juuh")
    private _$90812bn120893juuhBean _$90812bn120893juuh;
    @SerializedName("981273jn19203nj123rg")
    private _$981273jn19203nj123rgBean _$981273jn19203nj123rg;

    public _$987987M7812b163eryrtBean get_$987987M7812b163eryrt() {
        return _$987987M7812b163eryrt;
    }

    public void set_$987987M7812b163eryrt(_$987987M7812b163eryrtBean _$987987M7812b163eryrt) {
        this._$987987M7812b163eryrt = _$987987M7812b163eryrt;
    }

    public _$90812bn120893juuhBean get_$90812bn120893juuh() {
        return _$90812bn120893juuh;
    }

    public void set_$90812bn120893juuh(_$90812bn120893juuhBean _$90812bn120893juuh) {
        this._$90812bn120893juuh = _$90812bn120893juuh;
    }

    public _$981273jn19203nj123rgBean get_$981273jn19203nj123rg() {
        return _$981273jn19203nj123rg;
    }

    public void set_$981273jn19203nj123rg(_$981273jn19203nj123rgBean _$981273jn19203nj123rg) {
        this._$981273jn19203nj123rg = _$981273jn19203nj123rg;
    }

    public static class _$987987M7812b163eryrtBean {
        /**
         * id : 1
         * strong : 456
         * active : true
         * sell : te
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }

    public static class _$90812bn120893juuhBean {
        /**
         * id : 2
         * strong : 4700
         * active : true
         * sell : tt
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }

    public static class _$981273jn19203nj123rgBean {
        /**
         * id : 3
         * strong : 3000
         * active : true
         * sell : ti
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }
}}

1 个答案:

答案 0 :(得分:0)

您可以使用GSON。只需定义

@SerializedName("items")
Map<String, Inner> items;

在你的外部类中,Inner就像

那样的类
public class Inner {
    private int id;
    private boolean active;
    private int strong;
    private String sell;
}

或者您可以使用自定义Gson JsonDeserializer.