Reputation: 709
{
"page" : 0,
"pageSize" : 15,
"totalPageCount" : 5,
"data" : {
"020" : "Abarth",
"040" : "Alfa Romeo",
"042" : "Alpina",
"043" : "Alpine",
"057" : "Aston Martin",
"060" : "Audi",
"130" : "BMW",
"095" : "Barkas",
"107" : "Bentley",
"145" : "Brilliance",
"141" : "Buick",
"150" : "Cadillac",
"157" : "Caterham",
"160" : "Chevrolet",
"170" : "Chrysler"
}
}
I am getting a response like above and I want to parse it as Java Object using GSON, Please update me how should I parse this JSON?
Upvotes: 1
Views: 1217
Reputation: 10115
Solution:
Please follow the steps below.
First, create a model class:
public class MyResponse {
public int page;
public int pageSize;
public int totalPageCount;
public Map<String, String> data;
(make setter getter for page, pageSize, totalPageCount)
.......
.......
}
then, make a custom class as:
class RedirectionInfoDeserializer implements JsonDeserializer<MyResponse> {
private static final String KEY_PAGE = "page";
private static final String KEY_PAGESIZE = "pageSize";
private static final String KEY_TOTALPAGECOUNT = "totalPageCount";
private static final String KEY_DATA = "data";
@Override
public MyResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
// Read simple String values.
final String page = jsonObject.get(KEY_PAGE).getAsInt();
final String pageSize = jsonObject.get(KEY_PAGESIZE).getAsInt();
final String pageCount = jsonObject.get(KEY_TOTALPAGECOUNT).getAsInt();
// Read the dynamic parameters object.
final Map<String, String> data = readParametersMap(jsonObject);
RedirectionInfo result = new RedirectionInfo();
result.setUri(uri);
result.setHttpMethod(httpMethod);
result.setParameters(parameters);
return result;
}
@Nullable
private Map<String, String> readParametersMap(@NonNull final JsonObject jsonObject) {
final JsonElement paramsElement = jsonObject.get(KEY_DATA);
if (paramsElement == null) {
// value not present at all, just return null
return null;
}
final JsonObject parametersObject = paramsElement.getAsJsonObject();
final Map<String, String> parameters = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : parametersObject.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().getAsString();
data.put(key, value);
}
return data;
}
}
then make a method as:
private Converter.Factory createGsonConverter() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MyResponse.class, new RedirectionInfoDeserializer());
Gson gson = gsonBuilder.create();
return GsonConverterFactory.create(gson);
}
then, make this change in your Retrofit:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myserver.com")
.addConverterFactory(createGsonConverter())
.build();
That's it.
Try it, let me know if it works.
Upvotes: 2
Reputation: 503
first use below code :
YourPojo obj = new Gson().fromJson(json, YourPojo.class);
//YourPojo is the class you want json to convert to it.
//json is in String format and should be valid(use online validators like jsonlint.com)
second use link below:
Upvotes: 0
Reputation: 267
May be this will help you
private void parseJson(JSONObject data) {
if (data != null) {
Iterator<String> it = data.keys();
while (it.hasNext()) {
String key = it.next();
try {
if (data.get(key) instanceof JSONArray) {
JSONArray arry = data.getJSONArray(key);
int size = arry.length();
for (int i = 0; i < size; i++) {
parseJson(arry.getJSONObject(i));
}
} else if (data.get(key) instanceof JSONObject) {
parseJson(data.getJSONObject(key));
} else {
System.out.println(key + ":" + data.getString(key));
}
} catch (Throwable e) {
try {
System.out.println(key + ":" + data.getString(key));
} catch (Exception ee) {
}
e.printStackTrace();
}
}
}
}
Upvotes: 0