47059

Parsing JSON response using GSON with dynamic Key

Question:

This is how my JSON looks like and I have to parse the JSON, how can this be done using GSON

{ "data": { "a": { "abc": { "c": "d" } } } }

In which "a" is dynamic key which may vary from time to time. I am unable to find a solution right now

Answer1:

<strong>Model</strong>

public class Model { private HashMap<String, String> data; public Model() { } }

<strong>Convert json string to Hashmap using Gson & prepare data from hashmap</strong>

Gson gson = new Gson(); Type typeHashMap = new TypeToken<Map<String, String>>(){}.getType(); Map<String,String> map = gson.fromJson(YOUR_JSON, typeHashMap); Set<Map.Entry<String, String>> entrySet = data.entrySet(); Iterator iterator = entrySet.iterator (); for(int j = 0; j < entrySet.size(); j++) { try { Map.Entry entry = (Map.Entry) iterator.next(); String key = entry.getKey().toString(); String value = entry.getValue().toString(); //Add it to your list } catch(NoSuchElementException e) { e.printStackTrace(); } break; }

Answer2:

I am not sure if internal part abc is known to you or not. If it is known to you then you can surely do it with GSON. You have to create the class for the inner known object as below:

public class ABC { public C abc;}

Then create the Class for C:

public class C { public String c;}

Then just pass the ABC class as the hashmap value as below:

public HashMap<String, ABC> a;

Recommend

  • Program type already present: com.squareup.okhttp.Address
  • How to call a postgresql function from the JOOQ models in a select statement?
  • Java 8 LocalDate in Grails 3.2 JSON view
  • how to do a GET request using retrofit2?
  • kotlin convert json array to model list using GSON
  • Using GSON to parse Json into a JAVA Object where the Json Elements may vary
  • How to parse nested JSON with GSON
  • fromJson raises unparseable date exception
  • ExtJS 4 Spring 3 file upload. Server sends bad response content type
  • How to clear out the contents of a map when clear() method call throws UnsupportedOperationException
  • Update Search Results to Lazy Adapter in android
  • How to apply async task into this
  • Convert SQLite database to XML
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • Play WS (2.2.1): post/put large request
  • Different response to non-authenticated users and AJAX calls
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • JTable with a ScrollPane misbehaving
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?