1、遇到跨域问题时,前台无法调用别人提供的接口,则需要后台发送请求得到返回json格式数据,而我们又没有对应的类,只能当做String字符串对其进行处理,此时我们则需要对复杂json格式数据转换为map。
我们先输出json格式字符串。如:
{"rtState":true,"rtMsg":"设置成功,指令已下发","rtData":{"sid":1368631,"function":"19993","instructionType":"66AA","createTime":1564630209784,"createUserId":null,"createUserName":"127.0.0.1","updateTime":1564630209784,"equipmentIds":"0011613002A2","jobValue":"31","remark":null}}
上述字符串为标准的json格式字符串,一共两层;
2、使用代码对它进行处理:
首先我们要将json数据转换为数组,假设上述json格式字符段定义为josnString,执行代码
jsonString = "["+jsonString+"]";
public static Map JsonToMap(String jsonString ,int i){
JSONArray jsonArray = JSONArray.fromObject(jsonString );
List> mapListJson = (List)jsonArray;
Map stringObjectMap = mapListJson.get(i);
return stringObjectMap;
}
json为要处理的json格式字符串数组,i为数组中的第几个字段,一般使用0,因为我们是直接在json字符串前后加的“[]”,
返回结果即为map集合。
JSONArray.fromObject(String)方法依赖为import net.sf.json.JSONArray。
springboot添加依赖使用:
net.sf.json-lib
json-lib
2.4
jdk15
必须规定jdk版本,否则会报错。
执行代码
System.out.println(stringObjectMap.get("rtState"));
则会输出true;
3、一次处理只能得到第一层数据,想要得到第几层数据,则需要执行几次jsonToMap(String jsonString ,int i)方法。
譬如对以下json格式字符串进行处理,想要得到LED灯的状态“isValue”的值,则需要执行4次方法。
{"rtState":true,"rtMsg":"数据获取成功","rtData":{"instructionList":[{"serviceItem":"22BC","serviceContent":"0","equipmentSid":2171,"fieldUnit":"s","insValue":"0","serviceName":"传输间隔","equipmentId":"0011613002A2","fieldType":"NUMBER"},{"serviceItem":"66AA","serviceContent":"1","equipmentSid":2171,"fieldUnit":null,"insValue":"0","serviceName":"LED灯","equipmentId":"0011613002A2","fieldType":"BOOL"},{"serviceItem":"078F","serviceContent":"0","equipmentSid":2171,"fieldUnit":"","insValue":"0","serviceName":"继电器","equipmentId":"0011613002A2","fieldType":"BOOL"}],"sim":null,"equipmentInfo":{"sid":2171,"orderId":null,"productId":30,"custPid":"E09183BA6265","equipmentCategory":"ZF-L100","equipmentType":"模块","equipmentName":"共享租赁","equipmentId":"0011613002A2","batchNumber":"10103320170919","macAddress":"861358038262135","passcode":null,"logisticsNumber":null,"gatewayId":1,"gatewayName":null,"vendor":null,"uploadAddress":null,"positionInfo":"18041101","installTime":null,"phoneCardInfo":"89860038221705B01065","payTime":null,"equipmentElectricity":null,"equipmentStatus":"离线","warningStatus":"正常","instructionStatus":"正常","transmissionInterval":null,"bindStatus":"未绑定","custUserId":null,"custDeviceId":"0011613002A2","deviceId":"0011613002A2","bindTime":1517806279000,"deviceType":0,"bindEquipmentId":null,"createUserId":1,"createUserName":"administator","createTime":1508315676000,"updateTime":1564560090000,"isOneself":0,"remark":null}}}
执行下面代码测试结果:
token1="["+token1+"]";//token1即为要处理的json格式字段
Map stringObjectMap = JsonUtil.JsonToMap(token1,0);
Map stringObjectMap1 = JsonUtil.JsonToMap("[" + stringObjectMap.get("rtData") + "]",0);
//此处不需要加"[""]",是因为字段已经有了"[""]",1代表我们寻找的是数组第二个字段,即LED灯的数据。
Map stringObjectMap2 = JsonUtil.JsonToMap(stringObjectMap1.get("instructionList").toString(),1);
Map stringObjectMap3 = JsonUtil.JsonToMap("[" +stringObjectMap2+"]",0);
String insValue = (String) stringObjectMap3.get("insValue");
System.out.println(insValue);
后台输出结果为0则正确。