json对象如何转义为字符串

`
//实现请求参数类型均为string businessID、resultType、data的接口调用

	JSONObject contractRequesJson=new JSONObject();
	contractRequesJson.put("businessID","20200202");
	contractRequesJson.put("resultType","1");
	JSONObject dataJson=new JSONObject();
	dataJson.put("contractId","001");
	dataJson.put("contractType","AA");
	contractRequesJson.put("data",dataJson.toString());
	System.out.println("jsonObj-jsonObj"+contractRequesJson.toString());

	Map dataMap=new HashMap();
	dataMap.put("contractId","001");
	dataMap.put("contractType","AA");
	contractRequesJson.put("data",dataMap);
	System.out.println("jsonObj-map"+contractRequesJson.toString());

	Map contractRequestVo=new HashMap();
	contractRequestVo.put("businessID","20200202");
	contractRequestVo.put("resultType","1");
	contractRequestVo.put("data",dataJson.toString());
	System.out.println("map-jsonObject"+JSONObject.fromObject(contractRequestVo).toString());

	contractRequestVo.put("data",dataMap);
	System.out.println("map-map"+JSONObject.fromObject(contractRequestVo).toString());

`
请求参数打印:

JSONObject在外层嵌套map对象时,可以使jsonObject的对象转义成string字符串(层层嵌套时,最外层为map对象即可)
调用接口示例:

参数为某一对象时,参数类型须一一对应,用post application/json请求时允许通过
因此当有类型为string而非json对象格式时须对请求参数进行转义

原文地址:https://www.cnblogs.com/caichaoxiang919/p/13230013.html