3
SortedMap<String, VehicleData> hmap = new TreeMap<String, VehicleData>();

My JSON String sample:

 {
    "3": {
        "Field1": 12,
        "Field2": "value",
        "Field3": null
    },
    "test": {
        "Field1": 20,
        "Field2": "value",
        "Field3": "vit"
    }
}

I want to convert this string to HashMap declared above. Is there any method to convert directly from Json string to Hashmap?

Amit Gupta
  • 8,914
  • 1
  • 25
  • 33

1 Answers1

0

using Gson you can parse it eailsy at server side.

Gson gson = new Gson();
Type type= new TypeToken<Map<String, VehicleData>>(){}.getType();
Map<String,VehicleData> map = gson.fromJson(Your_JSON_STRING, type);

If you want client/server side serialize/deserialize JSON in GWT code.

In GWT 2.1.1 you can use GWT AutoBean framework

String serializeToJson(Test test) 
{
    // Retrieve the AutoBean controller
    AutoBean<Test> bean = AutoBeanUtils.getAutoBean(test);
    return AutoBeanCodex.encode(bean).getPayload();
}

Test deserializeFromJson(String json) 
{     
    AutoBean<Test> bean = AutoBeanCodex.decode(myFactory, Test.class, json);     
    return bean.as();   
} 

I've not tried with complex and low level with Map, you can go with doc

Finally if you want to gson on client side with GWT then you have to try bGwtGson library

Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
  • yeah its for Server-side Deserializing/serializing JSON with GWT not for the client. – Asif Bhutto Dec 04 '13 at 12:50
  • @Christian Kuetbach you can deserializing/serialize with GWT AutoBean framework at client/server side, but i have not tried with complex Java Map – Asif Bhutto Dec 04 '13 at 12:59
  • AutoBeans works, but they are some kind of buggy especially if you use Maps or Lists. Sometimes you will need to recreate the beans, f you modify the content of a map. And you should not work with concrete types. You should use the Map-Interface. In fact, I think AutoBeans are really buggy. – Christian Kuetbach Dec 04 '13 at 13:03
  • if you want to gson on client side then you have to try this library https://github.com/heroandtn3/bGwtGson – Asif Bhutto Dec 04 '13 at 13:06