0

I have a json file which has following structure:

{
"Otions": true
"Plastic": ""
"Houses": {
   7: {
   "name": "Akash Bhawan"
   "MemBers": 3
   "children": 1
   }-
   8: {
   "name": "Ashiyana"
   "memBers": 4
   "children": 2
   }-
   9: {
  "name": "Faruks Nest"
  "memBers": 5
  "children": 1
  }-

The objects inside Houses are variable and can increase or decrease accordingly and also change names. How ever the fieds "name", "members", "children" are the only fields and will always be there

i am using gson to parse

@SerializedName("Otions")
private String Options;
@SerializedName("Plastic")
private String plastics;
@SerializedName("Houses")
private Houses houses;

i want to know if there is a way we can i can store differently named objects in a hashtable or some other way?

Tushar Saha
  • 1,978
  • 24
  • 29
  • You might look into `names()` or `keys()` method of the `JSONObject`. I don't remember which ones gets your the keys (which will be "8" and "9" in your case). – Sufian Dec 08 '15 at 07:06
  • take a look at this [SO Thread](http://stackoverflow.com/questions/6796662/using-gson-to-parse-a-json-with-dynamic-key-and-value-in-android) – Kaushik Dec 08 '15 at 07:50

4 Answers4

1

If you can't change the structure then you have to make house as Hashmap of int to object. For Example:-

HashMap<int, Object> House;

And that object will have elements like name, memBers, details.

himanshu1496
  • 1,921
  • 19
  • 34
  • Do you mean JSONObject instead of Object ? I can't imagine an object having elements without a type – Prim Dec 08 '15 at 08:08
  • @Prim mean create a class with those three element and object of that class. – himanshu1496 Dec 08 '15 at 09:08
  • @TusharSaha it worked because above you have key-value pair, Where key is 7,8, 9 which are integers and value is { "name": "Akash Bhawan", "MemBers": 3, "children": 1 } which is an object, it is as simple as that. – himanshu1496 Dec 08 '15 at 09:12
  • @TusharSaha even if you want to go with an unnecessary HashMap-like solution, I'd suggest you to use `SparseArray`. You will avoid auto-boxing this way. – Sufian Dec 08 '15 at 09:22
0

Houses should be a map like this.

private HashMap houses.

Since the 3: indicates an index-structure. Gson will create the nested objects as needed.

Marcinek
  • 2,144
  • 1
  • 19
  • 25
0

Please make "house" as bunch of array instead of objects.

{
    "Options": true,
    "Plastics": "",
    "house": [{
        "name": "Akash Bhawan",
        "MemBers": 3,
        "children": 1
    }, {
        "name": "Ashiyana",
        "memBers": 4,
        "children": 2
    }, {
        "name": "Faruks Nest",
        "memBers": 5,
        "children": 1
    }]
}
himanshu1496
  • 1,921
  • 19
  • 34
0

You should do something like (as said in my comment):

JSONObject jsonHouses = jsonObject.getJSONObject("Houses");
Iterator<String> keys = jsonHouses.keys(); //this will be array of "7", "8", "9"

if (keys.hasNext()) {
    JSONObject singleHouseObject = jsonHouses.getJSONObject(keys.next());
    //now do whatever you want to do with singleHouseObject.
    //this is the object which has "name", "MemBers", "children"
}
Sufian
  • 6,405
  • 16
  • 66
  • 120