日期:2014-05-16  浏览次数:20394 次

Gson json to list or map
Gson gson = new Gson();
		
		//json to list
		Type listType = new TypeToken<List<String>>(){}.getType();
		List<String> list = new LinkedList<String>();
		list.add("blah");

		String listJson = gson.toJson(list, listType);
		List<String> newList = gson.fromJson(listJson, listType);
		System.out.println(newList.get(0));

		//json to map
		Type mapType = new TypeToken<Map<String, String>>() {}.getType();
		Map<String, String> map = new HashMap<String, String>();
		map.put("blah", "DESC");

		String mapJson = gson.toJson(map, mapType);
		Map<String, String> newMap = gson.fromJson(mapJson, mapType);
		System.out.println(newMap.get("blah"));
?

?