JacksonJsonParser是spring boot裏面的一個json類,他可以解析json數據,一開始發現spring boot中有json解析很高興,不用再去引入依賴,結果使用的時候才發現一個大坑。那就是解析複雜的嵌套json會致使引號丟失。java
public Object jsonUtil(String json,String key) { JacksonJsonParser jacksonJsonParser = new JacksonJsonParser(); Map<String,Object> map = jacksonJsonParser.parseMap(json); for(String keyString:map.keySet()) { if (keyString.equals(key)) { return map.get(key); } } return null; }
傳入json和本身所須要的value的key返回valuespring
當傳入的json是正確的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的內容code
{anglechange={msg=angle change, level=Error, source=Device, receivedDate=1554881337850, eventDate=1554852553000}, type=Alert}
這就致使了當我想再獲取剩下嵌套的value的時候,key都沒有引號報錯get