Java 编码 和JSON
Posted 太阳花神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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":"[email protected]@3","ym800_7.5_18031521":"[email protected]@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="[email protected]@SNXYQLyR";
String [] res=devices.split("@");
List a=new JSONArray();
for(int i=1;i<res.length;i++)
{
a.add(res[i]);
}
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)
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
HttpRequest request = HttpRequest.post(UNURL).contentType("application/json;charset=utf-8").send(xml);
returnxml=request.body();
以上是关于Java 编码 和JSON的主要内容,如果未能解决你的问题,请参考以下文章
错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”