-1

im looking to a simple way to parse this JSON into a Gson java class for easily way to put him into a adapter and fill some listviews the problem is the part of items,i dont know the items objects names, as you can see i have random objects names inside items, is possible do to this with GSON or i need another code logic, this is for Android, thanks for your help :

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

Add the code of GSON format to 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;
        }
    }
}}
LuDev
  • 9
  • 4
  • try looking into this https://stackoverflow.com/questions/20442265/how-to-decode-json-with-unknown-field-using-gson – Tomer Shemesh Jan 31 '18 at 19:40
  • Why does the backend need to send you gibberish keys, rather than them just being an array, if the items themselves already have IDs? – Submersed Jan 31 '18 at 19:51
  • Thanks @Submersed if with gibberish keys you mean 987987M7812b163eryrt, 90812bn120893juuh and 981273jn19203nj123rg is becouse is another id_numeric_string that the backend have. – LuDev Jan 31 '18 at 20:10

1 Answers1

0

You can use GSON. Just define

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

in your outer class where Inner is class like

public class Inner {
    private int id;
    private boolean active;
    private int strong;
    private String sell;
}

Or you can use a custom Gson JsonDeserializer.

Guru
  • 1,653
  • 1
  • 7
  • 14
  • Thanks, i will try this, anticipate i'm looking for something like this, that is, a simple java class that i can be able to call for fill a listview, not matter the quantity and don't matter the objects name. PD: i'm not to acquainted with custom Gson JsonDeserializer. – LuDev Jan 31 '18 at 20:05
  • Sorry this is actually not working, can't access to values – LuDev Mar 20 '18 at 21:10