• Java 编码 和JSON


    1.编码

    序列化(urlencode编码):经过urlencode编码

            String a="[{"clubNo":"10000002","clubType":"1"},{"clubNo":"10000003","clubType":"4"},{"clubNo":"10000004","clubType":"3"}]";

          将字符串a经过urlencode编码:  a = java.net.URLEncoder.encode(a,"utf-8");

    反序列化(urldecode解码)

    String a="%5b%7b%22clubNo%22%3a%2210000002%22%2c%22clubType%22%3a%221%22%7d%2c%7b%22clubNo%22%3a%2210000003%22%2c%22clubType%22%3a%224%22%7d%2c%7b%22clubNo%22%3a%2210000004%22%2c%22clubType%22%3a%223%22%7d%5d";

     a=java.net.URLDecoder.decode(a,"UTF-8");

    2.JSON

    (1)将多个字符串转换成json数据:

    String a="ert";
    String c="kkkk";
    JSONObject json=new JSONObject();
    json.put("a", a);
    json.put("c", c);
    System.out.println(json.toJSONString());

    {“a”:"ert","c":"kkkk"}

    (2)解析json型的字符串:

    String datas="{"imei":"358732036143010","version":"2.3","apk":[{"appname":"SyncKey","packagename":"com.discovery.synckey"},{"appname":"DbTest","packagename":"com.discovery.synckey"},{"appname":"AppUpdate","packagename":"com.discovery.synckey"}]}";

    JSONObject reqJson=JSONObject.parseObject(datas);

    String imei=reqJson.getString("imei");

    String apk=reqJson.getString("apk");//json型集合

    JSONArray parseArry =JSONObject.parseArray(apk);

    Iterator<Object> iterator =parseArry.iterator();

    while(iterator.hasNext())
    {
      JSONObject object =(JSONObject)iterator.next();

           String  appname =(String)object.getString("appname")

    }

    (3).

        原数据

    datas:  {"hma17-kme-180425dntn":"1531@1531@3","ym800_7.5_18031521":"745@744@369"}
    解析后的数据
    {"maxPage":2,"list":[{"name":"hma17-kme-180425dntn","num":"1531","actnum":"1531","weeknum":"3"},{"name":"ym800_7.5_18031521","num":"745","actnum":"744","weeknum":"369"}]}

    JSONObject reqJson=JSON.parseObject(datas);

    for(Map.Entry<String, Object> entry:reqJson.entrySet()){
      System.out.println("~!!!!!!!!!!!!!!!!!!!!!");
      System.out.println(entry.getKey());
      System.out.println(entry.getValue());

      String c=(String)entry.getValue();
      String [] res=c.split("@");

      String name=entry.getKey();

      String num=res[0];

      String actNum=res[1];

      String  weekNum=res[2];

    }

    (4)

    String  devices="hXqLTLv9@SyJ4KieG@SNXYQLyR";

    String [] res=devices.split("@");

    List a=new JSONArray();

      for(int i=1;i<res.length;i++)
      {
                  a.add(res[i]);
           }
        (5)

    原数据

    result: ["BuildInfo","t1a_v1.3_build201805230930","IMEI","358732036575930","buildModel","AIINCART1","cpuID","M186MY50SVPL13","cpuInfo","mt6737h:3:0xd03:0x0:7:0x41","deviceSN","0123456789ABCDEF","sysFeature","24","iccid","89861117147550357527"]

    解析后的数据

      {

        "data": {
            "sysFeature": "24",
            "iccid": "89861117147550357527",
            "cpuID": "M186MY50SVPL13",
            "BuildInfo": "t1a_v1.3_build201805230930",
            "IMEI": "358732036575930",
            "buildModel": "AIINCART1",
            "cpuInfo": "mt6737h:3:0xd03:0x0:7:0x41",
            "deviceSN": "0123456789ABCDEF"
           },
        "errorCode": "0000",
        "errorMsg": "成功获取设备信息"
      }

    result = result.substring(1, result.length() - 1);
            System.out.println("输出结果"+result);
             String deviceArray[] = result.split(",");
             List<String> deviceInfo = Arrays.asList(deviceArray);
             for (int i = 0; i < deviceInfo.size(); i += 2) {
                 String str = deviceInfo.get(i);
                
                    if (str.contains("BuildInfo")) {
                      BuildInfo=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("IMEI")) {
                     IMEI=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("buildModel")) {
                      buildModel=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("cpuID")) {
                       cpuID=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("cpuInfo")) {
                      cpuInfo=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("deviceSN")) {
                       deviceSN=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("sysFeature")) {
                         sysFeature=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      } else if (str.contains("iccid")) {
                         iccid=deviceInfo.get(i + 1).substring(1, deviceInfo.get(i + 1).length() - 1);
                      }
                 System.out.println("*******"+str);
             }    
             errorCode="0000";
             errorMsg="成功获取设备信息";
            }
    
    JSONObject json=new JSONObject();
             JSONObject out=new JSONObject();
             out.put("BuildInfo", BuildInfo);
             out.put("IMEI", IMEI);
             out.put("buildModel", buildModel);
             out.put("cpuID", cpuID);
             out.put("cpuInfo", cpuInfo);
             out.put("deviceSN", deviceSN);
             out.put("sysFeature", sysFeature);
             out.put("iccid", iccid);
             json.put("errorCode", errorCode);
             json.put("errorMsg", errorMsg);
             json.put("data", out);

    3.字符串不区分大小写进行比较

     if(res[i].toLowerCase().contains(deviceId.toLowerCase())) //不区分大小写比较是否相等

    club.replaceAll(""""", """); //去掉字符串club中的

    a=a.replace(""", "");//去掉字符串a中的"

    4.访问三方的接口

    Maven项目中要加依赖:

    <dependency>
      <groupId>com.bladejava</groupId>
      <artifactId>blade-kit</artifactId>
      <version>1.3.4</version>
    </dependency>

    (1)get

    url="http://"+ipNum+"/wxServer/GetOnlineDeviceID?num=ALL";

    HttpRequest req=HttpRequest.get(url);

    String content=req.body();  //三方接口返回的内容

    (2)post请求

    //openIdUrl  具体的url

    HttpRequest request = HttpRequest.post(openIdUrl).contentType("application/json;charset=utf-8");  
    String res = request.body();  //三方接口返回的内容

    JSONObject obj = JSON.parseObject(res);

    String access_token=obj.getString("access_token"); //三方接口中具体某个字段值

    (3)post请求带有数据型访问三方接口

            //UNURL  具体的url

             String xml="";

           HttpRequest request = HttpRequest.post(UNURL).contentType("application/json;charset=utf-8").send(xml);
           returnxml=request.body();

     

  • 相关阅读:
    点击cell后 cell的背景不变,cell上的字体颜色发生改变的功能实现
    各种属性设置
    多列表 ,菜单
    正则表达式
    多个storyboard之间的跳转问题
    关于uicollectionview的个人学习
    uiscrollview的自动布局
    手动自动布局
    关于简单的跳转问题
    深入理解@class和#import的区别
  • 原文地址:https://www.cnblogs.com/yangxiaomei/p/9019999.html
Copyright © 2020-2023  润新知