zoukankan      html  css  js  c++  java
  • java 调用三方接口post传参时map和jsonobject的区别

          如果方法参数param是要求以json字符串的形式传递则:

      1. 如果是JSONObject对象转字符串则:String result = HttpUtil.doPost(URL, json.toJsonString());

      2. Map转字符串则需采用:String result = HttpUtil.doPost(URL, JSON.toJSONString(map));

      注:使用map.toString() 时会出现参数解析不到的问题

      因为:json.toJsonString()转换后为:{"name":"ceshi","password":"123456"}

         map.toString()转换后为:{password=123456, name=ceshi}

      对比可知,参数不一致;

      测试方法如下:

    public static void main(String[] args) {
            Map map = new HashMap<>();
            map.put("name", "ceshi");
            map.put("password", "123456");
            System.out.println(map.toString()); //{password=123456, name=ceshi}
            System.out.println(JSON.toJSONString(map)); //{"name":"ceshi","password":"123456"}
    
            JSONObject json = new JSONObject();
            json.put("name", "ceshi");
            json.put("password", "123456");
            System.out.println(json.toJSONString()); //{"name":"ceshi","password":"123456"}
        }

    参考:https://www.cnblogs.com/mufengforward/p/10707484.html

  • 相关阅读:
    三个问题
    2014-7
    2014-5
    2014-2
    2014-1
    2013-11
    mysql中对表操作----为所有列插入数据
    Redis做消息队列
    收集Nginx-access,Nginx-error日志
    .Nginx安装filebeat收集日志:
  • 原文地址:https://www.cnblogs.com/personblog/p/14893331.html
Copyright © 2011-2022 走看看