Json array in hashmap using google gson

I am new to Gson and I am trying to Hashmap array of an object in Hashmap , but I get a com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3 .

My code

 Map<String, String> listOfCountry = new HashMap<String, String>(); Gson gson = new Gson(); Type listType = new TypeToken<HashMap<String, String>>() {}.getType(); listOfCountry = gson.fromJson(sb.toString(), listType); 

and json

 [ {"countryId":"1","countryName":"India"}, {"countryId":"2","countryName":"United State"} ] 
+7
source share
3 answers

Your JSON is an array of objects that is not like a HashMap .

If you mean that you are trying to convert this to a List from a HashMap ... then this is what you need to do:

 Gson gson = new Gson(); Type listType = new TypeToken<List<HashMap<String, String>>>(){}.getType(); List<HashMap<String, String>> listOfCountry = gson.fromJson(sb.toString(), listType); 

Edit to add from the comments below:

If you want to deserialize the Country POJO array (this is really the best approach), it is simple as:

 class Country { public String countryId; public String countryName; } ... Country[] countryArray = gson.fromJson(myJsonString, Country[].class); 

However, it is better to use Collection :

 Type listType = new TypeToken<List<Country>>(){}.getType(); List<Country> countryList = gson.fromJson(myJsonString, listType); 
+8
source

I assume that you are trying to create a mapping from countryId to countryName s, right? This can be done in Gson, but it’s not really what it is intended for. First of all, Gson translates JSON into equivalent Java objects (for example, an array into a list, an object into an object or map, etc.), and not to convert arbitrary JSON to arbitrary Java.

If possible, your best bet is to reorganize your JSON. Consider the following format:

 { "1": "India", "2": "United State" } 

This is less verbose, easier to read and, most importantly, easy to parse with Gson:

 Type countryMapType = new TypeToken<Map<Integer,String>>(){}.getType(); Map<Integer,String> countryMap = gson.fromJson(sb.toString(), countryMapType); 

If you cannot edit the JSON syntax, you will have to manually parse the JSON data into the structure you are trying to create, which will be somewhat tedious and involved. It is best to read the Gson User Guide to find out how to do this.

As an aside, you call your Map<String, String> listOfCountry object, which is a confusing name - is it a map or a list? Avoid using names such as "list" for objects that are not lists. In this case, I would suggest either countries or countryMap .

+1
source

After reading what @ dimo414 and @Brian Roach said, if you still want to get the map from your json structure, you can do this:

 Type type = new TypeToken<HashMap<String, String>>() {}.getType(); Gson gson = new GsonBuilder().registerTypeAdapter(type, new JsonDeserializer<HashMap<String, String>>() { @Override public HashMap<String, String> deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { HashMap<String, String> map = new HashMap<>(); for (JsonElement element : jsonElement.getAsJsonArray()) { JsonObject jsonObject = element.getAsJsonObject(); signals.put(jsonObject.get("countryId").getAsString(), jsonObject.get("countryName").getAsString()); } return map; } }).create(); HashMap<String, String> countries = gson.fromJson(jsonArray, type); 

But at this point, you can just parse your JSON in JsonArray and view it by creating a map.

0
source

All Articles