Christopher Wirt
Christopher Wirt

Reputation: 1116

Creating a Java Object from JSON Object containing JSON Maps using GSON

So, I've been doing GSON for a while, but I just now ran into the issue of using JSON Maps, which as I understand it are basically just Key Value pairs where he value is a JSON object.

To give you an idea where I'm coming from, here's my JSON

{
   "configs":[
      {
         "com.hp.sdn.adm.alert.impl.AlertManager":{
            "trim.alert.age":{
               "def_val":"14",
               "desc":"Days an alert remains in storage (1 - 31)",
               "val":"14"
            },
            "trim.enabled":{
               "def_val":"true",
               "desc":"Allow trim operation (true/false)",
               "val":"true"
            },
            "trim.frequency":{
               "def_val":"24",
               "desc":"Frequency in hours of trim operations (8 - 168)",
               "val":"24"
            }
         }
      },
      {
         "com.hp.sdn.adm.auditlog.impl.AuditLogManager":{
            "trim.auditlog.age":{
               "def_val":"365",
               "desc":"Days an audit log remains in storage (31 - 1870)",
               "val":"365"
            },
            "trim.enabled":{
               "def_val":"true",
               "desc":"Allow trim operation (true/false)",
               "val":"true"
            },
            "trim.frequency":{
               "def_val":"24",
               "desc":"Frequency in hours of trim operations (8 - 168)",
               "val":"24"
            }
         }
      }
   ]
}

All of those com.hp.sdn... things are dynamic, as in I won't know the key names until Runtime. I figured I can just use a HashMap for this and GSON would figure it out, but I'm not sure what I would name the field...


Here are my classes that I have so far

package com.wicomb.sdn.models.configs;

import java.util.HashMap;

import com.wicomb.sdn.types.Model;

public class ConfigResponse extends Model {
    private ConfigGroup[] configs;
}

package com.wicomb.sdn.models.configs;

import com.wicomb.sdn.types.Model;

public class ConfigGroup extends Model {
    private HashMap<String,Config> ????;
}

TL;DR How should I write the Java Class to let Gson know how to handle a Json property that I don't know the name of.. And lots of them.

Upvotes: 0

Views: 311

Answers (1)

mtrovo
mtrovo

Reputation: 879

You can feed Gson with a HashMap (or if children order is important a LinkedHashMap) than you iterate over the entries or the keys normally as you would do to any other map.

In the code below I use the following json as input:

{
 "test": 123,
 "nested":{
     "nested-1": 1,
     "nested-2": 2
     }
}

And the code looks like these:

public void testGson() {
    String input = "{\"test\": 123, \"nested\": {\"nested-1\": 1, \"nested-2\": 2}}";
    LinkedHashMap<String, Object> json = new Gson().fromJson(input, LinkedHashMap.class);

    // iterating
    for(Map.Entry<String, Object> entry : json.entrySet()){
        System.out.println(entry.getKey() + " -> " + entry.getValue());
    }

    // testing values 
    System.out.println(json.get("test")); // should be 123
    Map<String, Object> nested = (Map<String, Object>) json.get("nested");
    System.out.println(nested.get("nested-1")); // should be 1
    System.out.println(nested.get("nested-2")); // should be 2
}

Upvotes: 1

Related Questions