简体   繁体   English

将JSON转换为Hashmap <String, POJO> 使用GWT

[英]Converting JSON to Hashmap<String, POJO> using GWT

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

My JSON String sample: 我的JSON字符串示例:

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

I want to convert this string to HashMap declared above. 我想将此字符串转换为上面声明的HashMap。 Is there any method to convert directly from Json string to Hashmap? 有没有任何方法可以直接从Json字符串转换为Hashmap?

using Gson you can parse it eailsy at server side. 使用Gson,你可以在服务器端解析它。

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. 如果您希望客户端/服务器端在GWT代码中序列化/反序列化JSON。

In GWT 2.1.1 you can use GWT AutoBean framework 在GWT 2.1.1中,您可以使用GWT AutoBean框架

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 我没有尝试使用Map的复杂和低级别,你可以使用doc

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM