一、前言
调用第三方接口返回JSON树形结构
String json = "{\n" +" \"code\": \"1\",\n" +" \"msg\": \"成功\",\n" +" \"sub_code\": \"\",\n" +" \"sub_msg\": \"\",\n" +" \"biz_data\": {\n" +" \"appcode\": \"13115\",\n" +" \"msg\": \"人员业务校验有误:该人员有正在审核的申请,不能重复登记。;\",\n" +" \"tracerId\": \"68d43cda790897\",\n" +" \"success\": false\n" +" }\n" +"}";
二、使用JAR包
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId>
</dependency>
三、实现代码
使用去除转义符转阿里JSONObject
json = json.replace("\\n", "").replace("\\t", "").replace("\\", "");
JSONObject res = JSONObject.parseObject(StringEscapeUtils.unescapeJava(json));
使用jackson转Map
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = mapper.readValue(json, Map.class);
使用jackson转阿里JSONObject
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
JSONObject jsonObject = JSONObject.parseObject(String.valueOf(jsonNode));