If you really want to do this correctly, you'll need a custom JsonDeserializer. Like this:
public class CarDeserializer implements JsonDeserializer<Car> {
@Override
public Car deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Car car = new Gson().fromJson(json.toString(), Car.class);
try {
JSONObject object = new JSONObject(json.toString());
car.setCurrency(Currency.getInstance(object.getString("price_currency")));
car.setBalance(object.getJSONObject("balance").getInt("API Decode"));
JSONArray decodeArray = object.getJSONArray("decode");
for (int i = 0; i < decodeArray.length(); i++){
JSONObject decodeObject = (JSONObject) decodeArray.get(i);
if (decodeObject.get("label").equals("Make")){
car.setMake(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Manufacturer")){
car.setManufacturer(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Plant Country")){
car.setPlantCountry(decodeObject.getString("value"));
} else if (decodeObject.get("label").equals("Product Type")){
car.setProductType(decodeObject.getString("value"));
}
}
} catch (JSONException e){
Log.e("CarDeserializer", e.toString(), e);
}
return car;
}
}
The Car object looks like this:
public class Car {
private int price;
private transient Currency currency;
private transient int balance;
private transient String make;
private transient String manufacturer;
private transient String plantCountry;
private transient String productType;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getPlantCountry() {
return plantCountry;
}
public void setPlantCountry(String plantCountry) {
this.plantCountry = plantCountry;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
}
If Currency doesn't work for you, you can change that one to a String type like this:
@SerializedName("price_currency") private String currency;
And change the getter and setter accordingly.
If you have more objects in the decode array. You can add them as more branches in the deserializer.
This is used like this:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Car.class, new CarDeserializer());
Gson gson = gsonBuilder.create();
gson.fromJson(myJsonString, Car.class);
Note: The transient keyword in the Car class indicates to Gson that it shouldn't attempt to automatically parse the json for those fields.
Note 2: You'll need to include Gson in your project if you haven't already added it.