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();