package com.stackoverflow.q2496494;
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test {
public static void main(String... args) {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Gson gson = new Gson();
// Serialize.
String json = gson.toJson(map);
System.out.println(json); // {"key1":"value1","key2":"value2","key3":"value3"}
// Deserialize.
Map<String, String> map2 = gson.fromJson(json, new TypeToken<Map<String, String>>() {}.getType());
System.out.println(map2); // {key1=value1, key2=value2, key3=value3}
}
}
阅读(6971) | 评论(0) | 转发(0) |