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"}
        }
  • 相关阅读:
    Delphi 学习笔记
    Extjs 4
    面向对象(OOP)
    Java基础
    Ubantu(乌班图)
    CentOS 6.3操作常识
    英语音标单元音篇
    英语音标双元音篇
    英语音标辅音篇
    Oracle补习班第一天
  • 原文地址:https://www.cnblogs.com/mufengforward/p/10707484.html
Copyright © 2011-2022 走看看