<>1. 修改之前
DwxxDTO dwxxDTO = new DwxxDTO(); dwxxDTO.setEntname("测试公司"); dwxxDTO.setUniscid
("91110100000000000Y"); dwxxDTO.setOrgancode("MA0000000"); ZgcbdjDTO zgcbdjDTO =
new ZgcbdjDTO(); zgcbdjDTO.setCpdata(dwxxDTO); zgcbdjDTO.setType(type);
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(zgcbdjDTO); //方式一 Map<
String, String> body = new HashMap<String, String>(); body.put("sha1", zaiyao);
body.put("data", jsonObject.toString()); String bodyStr = JSONObject.
toJSONString(body); //方式一打印结果 { "sha1": "tdvbaaaaaaaaA=", "data":
"{\"type\":\"2\",\"cpdata\":{\"uniscid\":\"91110100000000000Y\",\"entname\":\"测试公司\",\"organcode\":\"MA0000000\"}}"
} //方式二 JSONObject body = new JSONObject(); body.put("sha1", zaiyao); body.put(
"data", jsonObject.toString()); String bodyStr = JSONObject.toJSONString(body);
//方式二打印结果 { "sha1": "tdvbaaaaaaaaA=", "data":
"{\"type\":\"2\",\"cpdata\":{\"uniscid\":\"91110100000000000Y\",\"entname\":\"测试公司\",\"organcode\":\"MA0000000\"}}"
}
<>2. 原因:

有可能是因为jsonboject中包含另一个json串,应该把里面那个json串提取出来,用fluenPut(key,value)方法放到jsonobject中;

<>3. 修改之后
DwxxDTO dwxxDTO = new DwxxDTO(); dwxxDTO.setEntname("测试公司"); dwxxDTO.setUniscid
("91110100000000000Y"); dwxxDTO.setOrgancode("MA0000000"); ZgcbdjDTO zgcbdjDTO =
new ZgcbdjDTO(); zgcbdjDTO.setCpdata(dwxxDTO); zgcbdjDTO.setType(type);
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(zgcbdjDTO); JSONObject
body= new JSONObject(); body.fluentPut("sha1",zaiyao); body.fluentPut("data",
jsonObject); System.out.println("body入参:" + bodyStr); //打印结果 { "sha1":
"tdvbaaaaaaaaA=", "data": { "type": "2", "cpdata": { "uniscid":
"91110100000000000Y", "entname": "测试公司", "organcode": "MA0000000" } } }
<>4. fluentPut源码:
public JSONObject fluentPut(String key, Object value) { map.put(key, value);
return this; }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信