1

I have to consume data from a service with following JSON fromat :

{
  "field1": {
    "key": "value"
  },
  "complexfield": {
    "name1": {
      "key": "value"
    },"name2": {
      "key": "value"
    },"name3": {
      "key": "value"
    },"name4": {
      "key": "value"
    },"name5": {
      "key": "value"
    },"name6": {
      "key": "value"
    },"name7": {
      "key": "value"
    }
  },
  "field2": {
    "key": "value"
  }
}

Here field1 & field2 are having constant format for which I can create a pojo, but for complexfield the inner name object will be different each time (i.e. format will be same but its name will change everytime). Also complexfield is not an array to iterate over it, its a object. One thing is clear I cannot create pojo for this I have to go Object by Object, but how am I suppose to fetch the complexfields inner object having different names in every response ? using getAsJsonObject("what to pass here as name ?") ?

OR

Can I iterate over such complex Object by getting name of each sub object @ runtime ?

Prashant
  • 4,474
  • 8
  • 34
  • 82
  • If the array of complexField nested POJOs is finite - you can make register a custom deserializer, otherwise - you can deserialize complexField into a Map, right? – Роман Гуйван Jan 04 '16 at 09:19
  • The major problem here complexfield is not an array its an JSON object. Also the format of this inner name object different in few cases. – Prashant Jan 04 '16 at 09:20
  • By "finite array of nested POJOs" I've meant the list of possible nested objects. You can register a custom deserializer for the whole object of yours and deserialize complexField as a Map http://stackoverflow.com/questions/6096940/how-do-i-write-a-custom-json-deserializer-for-gson Another example here: http://stackoverflow.com/questions/16590377/custom-json-deserializer-using-gson – Роман Гуйван Jan 04 '16 at 09:21
  • @ Роман Гуйван yep that's correct, but the name here is required for processing. Whatever appears within the object is dependent on its name. – Prashant Jan 04 '16 at 09:24
  • In case you deserialize regular fields as POJO fields and deserialize complexfield contents as Map - you will not face this problem, because writing your custom deserializer will allow you to process JSON as a tokken stream, so You'll definitely will be able to get all the key/value pairs from complexfield JSON object and put those into a map – Роман Гуйван Jan 04 '16 at 09:27
  • @РоманГуйван I tried working on your suggested thing it seems pretty complex to me can you create pojo for above, that will help me solve above issue ? – Prashant Jan 06 '16 at 10:20
  • http://www.javacreed.com/gson-deserialiser-example/ just follow this example – Роман Гуйван Jan 06 '16 at 11:23

1 Answers1

0

We finally ended up using entrySet() method of Gsons JsonObject class. Ref : http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/JsonObject.html#entrySet()

Where I get the key as strings and sub object as JsonElement.

How to use this ? Here is a stackoverflow thread :Iterate over JsonObject properties

Community
  • 1
  • 1
Prashant
  • 4,474
  • 8
  • 34
  • 82