2019独角兽企业重金招聘Python工程师标准>>>
JacksonJsonParser是spring boot里面的一个json类,他能够解析json数据,一开始发现spring boot中有json解析很高兴,不用再去引入依赖,结果使用的时候才发现一个大坑。那就是解析复杂的嵌套json会导致引号丢失。
public Object jsonUtil(String json,String key) {
JacksonJsonParser jacksonJsonParser = new JacksonJsonParser();
Map map = jacksonJsonParser.parseMap(json);
for(String keyString:map.keySet()) {
if (keyString.equals(key)) {
return map.get(key);
}
}
return null;
}
传入json和自己所需要的value的key返回value
当传入的json是正确的json数据返回的是一个‘假’的json,为什么说是假的呢,因为所有的引号都丢失了
{"did":"856018102A180006","measureData":{"roll":{"receivedDate":1554275432618,"value":"0.0","eventDate":1554275409000},"pitch":{"receivedDate":1554275432618,"value":"0.0","eventDate":1554275409000},"yaw":{"receivedDate":1554275432618,"value":"0.0","eventDate":1554275409000},"voltage":{"receivedDate":1554275432618,"value":"0.0","eventDate":1554275409000}},"alertData":{"anglechange":{"msg":"angle change","level":"Error","source":"Device","receivedDate":1554877853352,"eventDate":1554849068000},"type":"Alert"},"msgID":0}
我要获取alertData的内容
{anglechange={msg=angle change, level=Error, source=Device, receivedDate=1554881337850, eventDate=1554852553000}, type=Alert}
这就导致了当我想再获取剩下嵌套的value的时候,key都没有引号报错