当前位置: 代码迷 >> JavaScript >> Gson json to list or 地图
  详细解决方案

Gson json to list or 地图

热度:942   发布时间:2012-12-28 10:29:05.0
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"));
?

?

  相关解决方案