工作中遇到了一个关于解析Json串的问题 先上串
{
"timestamp": 1520941696,
"object": {
"_id": {
"$oid": "5aa7ba802c668f27832193d0"
},
"_class": "com.oneiotworld.evbdp.report.model.LRoutineReportData",
"vehicleUuid": "6b59b0d77d9d11e7b91ec850372410e5",
"VIN": "LN86GAAE1HB101109",
"dateTime": {
"$date": 1520941695000
},
"messageType1": "133",
"message1": "20",
"messageType2": "4",
"messageType3": "136",
"type": 15,
"gps": {
"dateTime": {
"$date": 1520941694000
},
"latitude": 22.675356,
"latitude_": "北纬",
"speed": 0,
"longitude": 113.820416,
"longitude_": "东经",
"location_valid": "无效定位",
"direction": "95",
"height": "0"
},
"vehicleStatus": {
"alarmStatus": "00000000",
"lampStatus": "01101000",
"doorStatus": "00000000",
"carLockStatus": "11111000",
"positionStatus": "00000000",
"otherStatus": "00010100"
},
"ubiInfo": {
"instantaneFuleCon": [
137,
15,
12,
12,
12,
12,
12,
12,
12,
12,
12,
12,
11,
11,
10
],
"tripAAvergeFuleCon": 2560,
"tripBAvergeFuleCon": 25344,
"instrumentDisplayMode": 99,
"remianOil": 512,
"totalMileage": 197031,
"rotateSpeed": [
28674,
-14334,
-30973,
28674,
-11006,
-12798,
-12030,
-10238,
-8446,
-5118,
-25854,
-4862,
-6142,
-5374,
-6654
],
"speed": [
-5888,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"steeringWheelPosition": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"steeringWheelSpeed": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"longitudinalAcceleration": [
0,
0,
0,
0,
16128,
0,
0,
0,
16384,
0,
0,
0,
0,
0,
0
],
"gearPositionSignal": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"tirePressure": {
"leftFrontTirePressureStatusEffectiveFlag": 0,
"leftFrontTirePressureStatus": 1,
"rightFrontTirePressureStatusEffectiveFlag": 1,
"rightFrontTirePressureStatus": 1,
"leftRearTirePressureStatusEffectiveFlag": 1,
"leftRearTirePressureStatus": 1,
"rightRearTirePressureStatusEffectiveFlag": 1,
"rightRearTirePressureStatus": 1,
"leftFrontTirePressure": 1,
"rightFrontTirePressure": 56,
"leftRearTirePressure": 56,
"rightRearTirePressure": 56
}
}
}
}
这样的一个字符串 要给它压成一层 只取有value 对应的key
我写了个递归的方法
public class JsonParsing{
public static void main(String[] args) {
String str = "{timestamp=1520941696, order=107, operation=i, database=leopaard.lRoutineReportData, object={ \"_id\" : { \"$oid\" : \"5aa7ba802c668f27832193d0\" }, \"_class\" : \"com.oneiotworld.evbdp.report.model.LRoutineReportData\", \"vehicleUuid\" : \"6b59b0d77d9d11e7b91ec850372410e5\", \"VIN\" : \"LN86GAAE1HB101109\", \"dateTime\" : { \"$date\" : 1520941695000 }, \"messageType1\" : \"133\", \"message1\" : \"20\", \"messageType2\" : \"4\", \"messageType3\" : \"136\", \"type\" : 15, \"gps\" : { \"dateTime\" : { \"$date\" : 1520941694000 }, \"latitude\" : 22.675356, \"latitude_\" : \"北纬\", \"speed\" : 0.0, \"longitude\" : 113.820416, \"longitude_\" : \"东经\", \"location_valid\" : \"无效定位\", \"direction\" : \"95\", \"height\" : \"0\" }, \"vehicleStatus\" : { \"alarmStatus\" : \"00000000\", \"lampStatus\" : \"01101000\", \"doorStatus\" : \"00000000\", \"carLockStatus\" : \"11111000\", \"positionStatus\" : \"00000000\", \"otherStatus\" : \"00010100\" }, \"ubiInfo\" : { \"instantaneFuleCon\" : [137, 15, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 10], \"tripAAvergeFuleCon\" : 2560.0, \"tripBAvergeFuleCon\" : 25344.0, \"instrumentDisplayMode\" : 99, \"remianOil\" : 512, \"totalMileage\" : 197031, \"rotateSpeed\" : [28674, -14334, -30973, 28674, -11006, -12798, -12030, -10238, -8446, -5118, -25854, -4862, -6142, -5374, -6654], \"speed\" : [-5888, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"steeringWheelPosition\" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"steeringWheelSpeed\" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"longitudinalAcceleration\" : [0, 0, 0, 0, 16128, 0, 0, 0, 16384, 0, 0, 0, 0, 0, 0], \"gearPositionSignal\" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"tirePressure\" : { \"leftFrontTirePressureStatusEffectiveFlag\" : 0, \"leftFrontTirePressureStatus\" : 1, \"rightFrontTirePressureStatusEffectiveFlag\" : 1, \"rightFrontTirePressureStatus\" : 1, \"leftRearTirePressureStatusEffectiveFlag\" : 1, \"leftRearTirePressureStatus\" : 1, \"rightRearTirePressureStatusEffectiveFlag\" : 1, \"rightRearTirePressureStatus\" : 1, \"leftFrontTirePressure\" : 1, \"rightFrontTirePressure\" : 56, \"leftRearTirePressure\" : 56, \"rightRearTirePressure\" : 56 } }}}";
Map
diguiFunction(map);
}
private static void diguiFunction(Map
for(Entry
String key = entry.getKey();
Object value = entry.getValue();
if(value instanceof JSONObject){
JSONObject jsonObject = (JSONObject) value;
diguiFunction((Map)jsonObject);
}else if(value instanceof JSONArray){
JSONArray array = (JSONArray)value;
}else{
}
}
}
}
依赖