How To Parse A Json With Dynamic “key” In Android By Using Gson
Solution 1:
How would you like your model class to look?
status
and total
would probably be int
, so that only leaves info
.
As an experiment, just add a field Object info
and see how Gson would set it to an ArrayList<LinkedHashMap<String, String>>
-- ugly and hard to access by key, but all the data is there. Given that information, the fastest way to model a class would be:
classSomething{
int status;
List<Map<String, String> info;
int total;
}
If you have control over how that JSON is generated, I suggest changing the structure of info
from an array of objects [{a:b},{c:d},{e:f}]
to just an object {a:b,c:d,e:f}
. With this, you could just map it to a Map<String, String>
with all the benefits like access by key, keys()
and values()
:
classSomething{
int status;
Map<String, String> info;
int total;
}
If you want the latter model class without changing the JSON format, you'll have to write a TypeAdapter
(or JsonDeserializer
if you're only interested in parsing JSON, not generating it from your model class).
Here's a JsonDeserializer hat would map your original info
JSON property to a plain Map<String, String>
.
classArrayOfObjectsToMapDeserializerimplementsJsonDeserializer<Map<String, String>> {
publicMap<String, String> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Map<String, String> result = newHashMap<String, String>();
JsonArray array = json.getAsJsonArray();
for (JsonElement element : array) {
JsonObjectobject = element.getAsJsonObject();
// This does not check if the objects only have one property, so JSON// like [{a:b,c:d}{e:f}] will become a Map like {a:b,c:d,e:f} as well.for (Entry<String, JsonElement> entry : object.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().getAsString();
result.put(key, value);
}
}
return result;
}
}
You need to register this custom JsonDeserializer
similar to this:
GsonBuilder builder = newGsonBuilder();
builder.registerTypeAdapter(
newTypeToken<Map<String, String>>() {}.getType(),
newArrayOfObjectsToMapDeserializer());
Gson gson = builder.create();
Note that this registers the custom deserializer for any Map<String, String>
regardless in what class it is encountered. If you don't want this, you'll need to create a custom TypeAdapterFactory
as well and check the declaring class before returning and instance of the deserializer.
Solution 2:
Here goes a solution, which does not requires to make a JsonDeserializer.
All you can create is a JsonElement in a map Map.Entry<String, JsonElement>
and use a for loop to iterate over the entries
//parsing string response to json objectJsonObjectjsonObject= (JsonObject) newJsonParser().parse(jsonString);
//getting root objectJsonObjectdateWiseContent= jsonObject.get("rootObject").getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : dateWiseContent.entrySet()) {
//this gets the dynamic keysStringdateKey= entry.getKey();
//you can get any thing now json element,array,object according to json.JsonArrayjsonArrayDates= entry.getValue().getAsJsonArray();
}
read more here
Post a Comment for "How To Parse A Json With Dynamic “key” In Android By Using Gson"