zoukankan      html  css  js  c++  java
  • 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"}]

  • 相关阅读:
    学习进度条
    学术诚信与职业道德
    czxt
    操作系统
    04 17评论博客
    0414 结对 2.0 33 34
    0408 汉堡包
    (补)结对心得
    构建之法4读后感
    复利计算4.0
  • 原文地址:https://www.cnblogs.com/xiaoyao-001/p/12888214.html
Copyright © 2011-2022 走看看