• JSONObject的问题- 在用JSONObject传参到controller接收为空白和JSONArray添加json后转string不正确


    问题一:

    在用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"}]

  • 相关阅读:
    [ Luogu 3398 ] 仓鼠找sugar
    [ JLOI 2014 ] 松鼠的新家
    AtcoderGrandContest 005 F. Many Easy Problems
    Codeforces 388 D. Fox and Perfect Sets
    Codeforces 1037 H. Security
    「学习笔记」wqs二分/dp凸优化
    「NOI2017」游戏
    「SCOI2014」方伯伯的商场之旅
    「SCOI2015」情报传递
    「SCOI2016」美味
  • 原文地址:https://www.cnblogs.com/xiaoyao-001/p/12888214.html
Copyright © 2020-2023  润新知