Parse JSON objects with random names

1

I'm trying to pair objects JSON with GSON I'm used to knowing the names of the objects, I create the classes and I just have to call those classes to use the objects and build custom adapters and finally fill lists or other views easily with GSON.

In this example I do not know the names of the JSON objects since they are quite random and it is not practical to create classes with those names besides that there is not a certain amount of objects, that is, it can be only 1 or More than 10 for example, is there any way in which you can create a java class that contains the information of the objects and can create lists or other views using or calling that class without knowing the names?

I've read in Stackoverflow something about HashMap or Map as well as Gson deserealizer customized but I'm not familiar with it, is it possible to use GSON or should I implement some other logic ?, Thanks in advance, I leave the example of the JSON, the question is in the object "items" as you can notice is not known the amount and the name of the object because they are random, this is for Android:

[
{
    "id": 1001,
    "name": "Super1",
    "user": {
        "character": "The Super 1"
    },
    "items": {
        "987987M7812b123": {
            "id": 1,
            "strong": 456,
            "active": true,
            "set": "tier1"
        },
        "90812bn120893": {
            "id": 2,
            "strong": 4700,
            "active": true,
            "set": "vex"
        },
        "981273jn19203nj": {
            "id": 3,
            "strong": 3000,
            "active": true,
            "set": "nesp"
        }
    }
}
]

I add the code of the class where I implement GSON:

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

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 {
    /**
     * character : The Super 1
     */

    private String character;

    public String getCharacter() {
        return character;
    }

    public void setCharacter(String character) {
        this.character = character;
    }
}

public static class ItemsBean {
    /**
     * 987987M7812b123 : {"id":1,"strong":456,"active":true,"set":"tier1"}
     * 90812bn120893 : {"id":2,"strong":4700,"active":true,"set":"vex"}
     * 981273jn19203nj : {"id":3,"strong":3000,"active":true,"set":"nesp"}
     */

    @SerializedName("987987M7812b123")
    private _$987987M7812b123Bean _$987987M7812b123;
    @SerializedName("90812bn120893")
    private _$90812bn120893Bean _$90812bn120893;
    @SerializedName("981273jn19203nj")
    private _$981273jn19203njBean _$981273jn19203nj;

    public _$987987M7812b123Bean get_$987987M7812b123() {
        return _$987987M7812b123;
    }

    public void set_$987987M7812b123(_$987987M7812b123Bean _$987987M7812b123) {
        this._$987987M7812b123 = _$987987M7812b123;
    }

    public _$90812bn120893Bean get_$90812bn120893() {
        return _$90812bn120893;
    }

    public void set_$90812bn120893(_$90812bn120893Bean _$90812bn120893) {
        this._$90812bn120893 = _$90812bn120893;
    }

    public _$981273jn19203njBean get_$981273jn19203nj() {
        return _$981273jn19203nj;
    }

    public void set_$981273jn19203nj(_$981273jn19203njBean _$981273jn19203nj) {
        this._$981273jn19203nj = _$981273jn19203nj;
    }

    public static class _$987987M7812b123Bean {
        /**
         * id : 1
         * strong : 456
         * active : true
         * set : tier1
         */

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

        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 getSet() {
            return set;
        }

        public void setSet(String set) {
            this.set = set;
        }
    }

    public static class _$90812bn120893Bean {
        /**
         * id : 2
         * strong : 4700
         * active : true
         * set : vex
         */

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

        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 getSet() {
            return set;
        }

        public void setSet(String set) {
            this.set = set;
        }
    }

    public static class _$981273jn19203njBean {
        /**
         * id : 3
         * strong : 3000
         * active : true
         * set : nesp
         */

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

        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 getSet() {
            return set;
        }

        public void setSet(String set) {
            this.set = set;
        }
    }
}}
    
asked by LuDev 31.01.2018 в 21:38
source

1 answer

1

What you can do is the following

  • What I see is that if you have a defined structure, since you have user, items, etc.

  • I think what you mean is that in the case of the items, you do not know what name they may have, for example 987987M7812b123 and you do not want to create a class for each one. If that is the case, you can try the following:

Create an object that supports all the attributes of your items (id, strong, active), for example, let's assume that the class is called attributes :

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class atributos {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("strong")
@Expose
private Integer strong;
@SerializedName("active")
@Expose
private Boolean active;
@SerializedName("set")
@Expose
private String set;

public Integer getId() {
return id;
}

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

public Integer getStrong() {
return strong;
}

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

public Boolean getActive() {
return active;
}

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

public String getSet() {
return set;
}

public void setSet(String set) {
this.set = set;
}

}

Then you can pause it in the following way:

@SerializedName("items")
@Expose
private HashMap<String, atributos> items;

I hope it helps you friend:)

    
answered by 31.01.2018 в 23:48