I am trying to use TypedArray to parse json.
My json will be the following:
{
"id":"112233",
"tag":"From server",
"users":{
"vijay":1,
"dhas":2,
"vijaydhas":3
}
}
Here the in the users object key will be dynamic. I will receive from the server at run time. On that time only I Don't know the key(vijay, dhas, vijaydhas).
To parse id and tag I will do the following code.
@Override
public MagazineReader read (JsonReader in) throws IOException {
final MagazineReader magazineReader = new MagazineReader();
in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "id":
magazineReader.setID(in.nextInt());
break;
case "tag":
magazineReader.setTag(in.nextString());
break;
in.beginArray();
/*
For User how to read the json???
*/
}
in.endObject();
}
Now I want to read and parse the users JsonArray and its object without knowing the key. I know how to parse the JSONObject without knowing the key.
JSONObject users= obj.getJSONObject("users");
Iterator iteratorObj = detailList.keys();
while (iteratorObj.hasNext())
{
String jsonKey = (String)iteratorObj.next();
property.put(jsonKey,usersList.get(jsonKey));
}
But in JsonReader I don't know how to read the json value without knowing key. Please help me on this. [1]: https://javacreed.com/gson-typeadapter-example