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

    post方法名及参数为:(具体方法可参考https://www.cnblogs.com/mufengforward/p/10510337.html)

      public static String doPost(String httpUrl, String param) {   ...   }

    如果方法参数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"}
        }
  • 相关阅读:
    string的sizeof
    计算程序运行时间
    sleep所在头文件
    Mysql复制表结构、表数据
    UIView属性
    UITextView
    UITextField属性
    UISwitch属性
    UISlide属性
    UISegment属性
  • 原文地址:https://www.cnblogs.com/mufengforward/p/10707484.html
Copyright © 2011-2022 走看看