问题一:
在用JSONObject传参到controller接收为空白的问题:
@RestController
@RequestMapping("/callback")
public class CallbackApiController {
Logger logger = LoggerFactory.getLogger(this.getClass());
@PostMapping("/callBackJson")
public void callBackJSON(@RequestBody org.json.JSONObject jsonObject){
logger.info("传递参数,param={}",jsonObject.toString());
logger.info("-------->回调成功!<------------");
}
}
运行结果:
2020-05-14 13:53:47.398 INFO 32904 --- [io-16016-exec-1] c.t.c.controller.CallbackApiController : 传递参数,param={}
2020-05-14 13:53:47.398 INFO 32904 --- [io-16016-exec-1] c.t.c.controller.CallbackApiController : -------->回调成功!<------------
将JSONObject的类型从
org.json.JSONObject
替换为
com.alibaba.fastjson.JSONObject
的运行成功,结果如下:
2020-05-14 13:56:09.585 INFO 33684 --- [io-16016-exec-2] c.t.c.controller.CallbackApiController : 传递参数,param={"url":"www.baidu.com"}
2020-05-14 13:56:09.585 INFO 33684 --- [io-16016-exec-2] c.t.c.controller.CallbackApiController : -------->回调成功!<------------
问题二
public static void main(String[] args) {
JSONObject json = new JSONObject();
JSONArray array = new JSONArray();
json.put("mm","22");
json.put("timestamp","jljlj");
array.add(json);
json.clear();
json.put("mm","jjj");
json.put("timestamp","oooo");
array.add(json);;
System.out.println(array.toJSONString());
}
运行结果[{"mm":"jjj","timestamp":"oooo"},{"$ref":"$[0]"}]
这样导致传送数据给第三方的接口时总是报错500解析不了,这是第二个加入的是第一个的引用,索引应该创建两个对象加入array中,如下正常
public static void main(String[] args) {
JSONObject json = new JSONObject();
JSONObject json2 = new JSONObject();
JSONArray array = new JSONArray();
json.put("mm","22");
json.put("timestamp","jljlj");
json2.put("mm","jjj");
json2.put("timestamp","oooo");
array.add(json);
array.add(json2);
System.out.println(array.toJSONString());
}
运行结果:[{"mm":"22","timestamp":"jljlj"},{"mm":"jjj","timestamp":"oooo"}]