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

我的 JSON 字符串示例:

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

我想将此字符串转换为上面声明的 HashMap。有什么方法可以直接从 Json 字符串转换为 Hashmap?

4

1 回答 1

0

使用 Gson 你可以在服务器端解析它。

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

如果您希望客户端/服务器端在 GWT 代码中序列化/反序列化 JSON。

在 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();   
} 

我没有尝试过使用 Map 进行复杂和低级别的操作,您可以使用 doc

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

于 2013-12-04T11:56:56.683 回答