处理Json,将json转成List>

利用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

利用ObjectMapper类,可将json字符串转成Lis >。 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 > 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
经验分享 程序员 微信小程序 职场和发展