1

My json is :

{"array":[{"US":"id_123"},{"UK":"id_112"},{"EN":"id_1112"}...]}

My classes are:

class LocaleResponce implements Serializable{
    @SerializedName("array")
    List<Locale> array;
}


class Locale implements Serializable{
    @SerializedName("title")
    String title;
    @SerializedName("id")
    String id;
}

I'm tried to make this:

Gson gson = new Gson();
Type type = new TypeToken<LocaleResponce >(){}.getType();
LocaleResponce response = gson.fromJson(cacheJsonObject.toString(), type);

it doesn't work or is it an issue of server?

2
  • It doesn't work because your json doesn't have title and id. If you have control over json better to change it like {"title":"US", "id":"id_123"} Commented Mar 3, 2016 at 14:05
  • 1
    I think you can use a HashMap if you have variable keys. Commented Mar 3, 2016 at 14:11

2 Answers 2

4

It can be achieved by creating custom JsonDeserializer. Your deserializer class will look something like

public class CityListDeserializer implements JsonDeserializer<List<City>>{

        @Override
        public List<City> deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            List<City> cityList = new ArrayList<>();

            JsonObject parentJsonObject = element.getAsJsonObject();
            Map.Entry<String, JsonElement> entry = parentJsonObject.entrySet().iterator().next();

            Iterator<JsonElement> iterator = entry.getValue().getAsJsonArray().iterator();
            City city;
            while (iterator.hasNext()){
                JsonObject cityJsonObject = iterator.next().getAsJsonObject();
                for(Map.Entry<String, JsonElement> entry1 : cityJsonObject.entrySet()){
                    city = new City();
                    city.cityName = entry1.getKey();
                    city.id = entry1.getValue().toString();
                    cityList.add(city);
                }
            }
            return cityList;
        }
    }

You can use it with

try {
            JSONObject object = new JSONObject("{\"array\":[{\"US\":\"id_123\"},{\"UK\":\"id_112\"},{\"EN\":\"id_1112\"}]}");
            GsonBuilder builder = new GsonBuilder();
            builder.registerTypeAdapter(new TypeToken<ArrayList<City>>() {}.getType(), new CityListDeserializer());
            Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
            List<City> cityList = gson.fromJson(String.valueOf(object), new TypeToken<ArrayList<City>>() {}.getType());
        } catch (JSONException e) {
            e.printStackTrace();
        }

Your City class will be

public class City {
        String cityName;
        String id;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can generate classes for Json using websites like http://www.jsonschema2pojo.org/ etc in your case the array array variable, does not have an array of homogeneous objects {"US":"id_123"},{"UK":"id_112"},{"EN":"id_1112"} these are all objects of different DataTypes because, the parameter keys are different, so for parsing this you cannot use a Pojo. The parameters vary from UK US EN etc, the solution here is either to ask the person who is developing the api to send Json that is Consistent, the array that you have received is not type safe, if you want to use this in Java you have to write lots of lines of code. For example you can get the value of the parameter "UK" like this

cacheJsonObject.get("array").getAsJsonArray().get(1).get("UK").getAsString();

This would return the value id_112 for instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.