处理Json,将json转成List<Map<String,Object>>

利用ObjectMapper类,可将json字符串转成Lis<Map<String,Object>>。

 

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

 

String json = "{ \"1\":"

+ "\"a\""

+ ", \"2\":"

+ " \"b\" "

+ ", \"3\":"

+ " \"c\" "

+ "},"

+ "{ \"1\":"

+ "\"a\""

+ ", \"2\":"

+ " \"b\" "

+ ", \"3\":"

+ " \"c\" "

+ "},"

+ "{ \"1\":"

+ "\"a\""

+ ", \"2\":"

+ " \"b\" "

+ ", \"3\":"

+ " \"c\" "

+ "},";

 

System.out.println("json: "+json);

String s_json = "[" +json.substring(0, json.length()-1)+ "]";

System.out.println("s_json: "+s_json);

ObjectMapper objectMapper = new ObjectMapper();

 

@SuppressWarnings("unchecked")

List<Map<String, Object>> readValue = objectMapper.readValue(s_json, List.class);

System.out.println(readValue);

}

最终结果:[{1=a, 2=b, 3=c}, {1=a, 2=b, 3=c}, {1=a, 2=b, 3=c}]

注:需要导入jackson-databind-2.5.0.jar,和jackson-core-2.5.0.jar

 

猜你喜欢

转载自1960370817.iteye.com/blog/2326279