Gson解析json字符串到Map

 

public class TmpTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final Map map = new LinkedHashMap();
		map.put("a", 1);
		map.put("b", "a");
		map.put("c", 2);
		final Gson gson = new Gson();
		final String string = gson.toJson(map);
		final Type type = new TypeToken>() {}.getType();
		final Map map2 = gson.fromJson(string, type);
		for (final Entry entry : map2.entrySet()) {
			System.out.println(entry.getKey() + " : " + entry.getValue());
		}
	}

}

你可能感兴趣的:(java,gson)