map集合转为JSON字符串

mybatis查询返回的map集合转换为JSON,并在外面嵌套相关节点
之前有尝试过用JSONObject.put字符串方法将额外\字符添加到JSON字符串
jsonobject.put(“message”, map);但是最后输出的值却不是想要的JSON字符串,会多出一些转义符,类似下面这种结构:

json object = {"details":"{\"products\":[{\"pid\":\"14\",\"name\":\"zxc\",\"price\":\"123456\"},{\"pid\":\"6\",\"name\":\"Sony Xperia\",\"price\":\"35000\"},{\"pid\":\"8\",\"name\":\"Samsung Galaxy Note\",\"price\":\"32000\"},{\"pid\":\"5\",\"name\":\"htc\",\"price\":\"26326\"},{\"pid\":\"9\",\"name\":\"Nokia Lumia 800\",\"price\":\"18000\"},{\"pid\":\"2\",\"name\":\"iphone\",\"price\":\"12345\"},{\"pid\":\"15\",\"name\":\"sdjnas\",\"price\":\"12243\"},{\"pid\":\"13\",\"name\":\"Samsung S5222\",\"price\":\"6500\"},{\"pid\":\"11\",\"name\":\"Nokia C201\",\"price\":\"4400\"},{\"pid\":\"7\",\"name\":\"Nokia Asha 200\",\"price\":\"4000\"},{\"pid\":\"1\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"3\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"4\",\"name\":\"htc\",\"price\":\"1234\"},{\"pid\":\"10\",\"name\":\"aks\",\"price\":\"1234\"},{\"pid\":\"12\",\"name\":\"asd\",\"price\":\"123\"}],\"success\":1}\n"}

测来测去还是直接用fastjson中的JSON.toJSONString()方法最简单直接,将查出来的集合放入map中,再通过fastjson转换,输出的json格式简单明了不容易出错,示例如下:
所需依赖:fastjson*


    com.cdxt.his
    fastjson
    1.2.83
 
  import com.alibaba.fastjson.JSON;
   /**
     * 封装转换json之前的map数据--成功
     */
    public static  String  getSuccessResp(HashMap<String,Object> map){
        HashMap<String ,Object> map1=new HashMap<>();
        HashMap<String ,Object> map2=new HashMap<>();
        map1.put("data",map);
        map1.put("code","1");
        map1.put("message","成功");
        map2.put("response",map1);
        String resultStr= JSON.toJSONString(map2);
        return resultStr;
    }
{
	"response": {
		"message": "成功",
		"data": {
			"item": [{
				"patient_name": "陆心莲",
				"visit_doctor_name": "张蓉",
				"reg_time": "2020-01-01 14:08:30",
				"reg_level_code": "41ab76501300ee8c69de",
				"register_code": "5fc805cd6e78808132f8",
				"reg_level_name": "副主任医师挂号费",
				"reg_fee": 11,
				"visit_doctor_code": "10047",
				"reg_state": "1",
				"visit_dept_code": "f48f97b5035b80810094",
				"visit_time": "2020-01-01 14:25:41"
			}, {
				"patient_name": "陆心莲",
				"visit_doctor_name": "罗蓉",
				"reg_time": "2020-01-02 15:39:46",
				"reg_level_code": "ddce766e430380816ffc",
				"register_code": "6546e166396d808169db",
				"reg_level_name": "普通门诊挂号费(主治医师)",
				"reg_fee": 9,
				"visit_doctor_code": "10194",
				"reg_state": "1",
				"visit_dept_code": "f48f97b5035b80810094",
				"visit_time": "2020-01-02 16:02:21"
			}, {
				"patient_name": "陆心莲",
				"visit_doctor_name": "罗蓉",
				"reg_time": "2020-01-16 10:03:32",
				"reg_level_code": "ddce766e430380816ffc",
				"register_code": "ac1d0b451f9580815150",
				"reg_level_name": "普通门诊挂号费(主治医师)",
				"reg_fee": 9,
				"visit_doctor_code": "10194",
				"reg_state": "1",
				"visit_dept_code": "f48f97b5035b80810094",
				"visit_time": "2020-01-16 10:09:42"
			}]
		},
		"code": "1"
	}
}

学习记录,仅供参考,谢谢。。。。

你可能感兴趣的:(json,mybatis,java)