Json.fromJson(Class type, CharSequence cs)
String json = "{\"uuid\": \"92955447-b781-42f6-acc3-1ed774c3acfb\",\"request\": \"{\\\"data\\\":[{\\\"device_id\\\":\\\"plate-aaa4-4cce-b487-0c56d7dfa03e\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"ahead-7a0d-4aef-9da8-8f8a807a900f\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"left-9667-4266-be34-d481032129fa\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"right-c27c-415d-bcb8-9ceb1e53d678\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"behind-88be-4424-a52a-c4ed74951448\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"}],\\\"device_class\\\":\\\"CAMERA\\\",\\\"type\\\":\\\"STATUS_REPORT\\\",\\\"uuid\\\":null}\"}";
System.out.println(json);
NutMap deviceStatusMap = Json.fromJson(NutMap.class, json);
JsonFormat jsonFormat= JsonFormat.tidy();
String jsonStr = Json.toJson(requestMap, jsonFormat);
System.out.println(jsonStr );
String json = "{\"uuid\": \"92955447-b781-42f6-acc3-1ed774c3acfb\",\"request\": \"{\\\"data\\\":[{\\\"device_id\\\":\\\"plate-aaa4-4cce-b487-0c56d7dfa03e\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"ahead-7a0d-4aef-9da8-8f8a807a900f\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"left-9667-4266-be34-d481032129fa\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"right-c27c-415d-bcb8-9ceb1e53d678\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"},{\\\"device_id\\\":\\\"behind-88be-4424-a52a-c4ed74951448\\\",\\\"status\\\":\\\"OK\\\",\\\"status_str\\\":\\\"正常\\\"}],\\\"device_class\\\":\\\"CAMERA\\\",\\\"type\\\":\\\"STATUS_REPORT\\\",\\\"uuid\\\":null}\"}";
System.out.println(json);
NutMap deviceStatusMap = Json.fromJson(NutMap.class, json);
NutMap requestMap = deviceStatusMap.getAs("request", NutMap.class);
JsonFormat jsonFormat= JsonFormat.tidy();
String jsonStr = Json.toJson(requestMap, jsonFormat);
System.out.println("isNullAsEmtry--1--"+jsonFormat.isNullAsEmtry());
jsonFormat.setNullAsEmtry(true);
System.out.println("isNullAsEmtry--2--"+jsonFormat.isNullAsEmtry());
System.out.println(jsonStr );
原json中我设置了 request
中的 uuid
字段值为null,打印结果如下:
原json----{“uuid”: “92955447-b781-42f6-acc3-1ed774c3acfb”,“request”: “{“data”:[{“device_id”:“plate-aaa4-4cce-b487-0c56d7dfa03e”,“status”:“OK”,“status_str”:“正常”},{“device_id”:“ahead-7a0d-4aef-9da8-8f8a807a900f”,“status”:“OK”,“status_str”:“正常”},{“device_id”:“left-9667-4266-be34-d481032129fa”,“status”:“OK”,“status_str”:“正常”},{“device_id”:“right-c27c-415d-bcb8-9ceb1e53d678”,“status”:“OK”,“status_str”:“正常”},{“device_id”:“behind-88be-4424-a52a-c4ed74951448”,“status”:“OK”,“status_str”:“正常”}],“device_class”:“CAMERA”,“type”:“STATUS_REPORT”,“uuid”:null}”}
isNullAsEmtry–1--false
isNullAsEmtry–2--true
设备状态–requestMap–:{“data”:[{“device_id”:“plate-aaa4-4cce-b487-0c56d7dfa03e”,“status”:“OK”,“status_str”:“正常”}, {“device_id”:“ahead-7a0d-4aef-9da8-8f8a807a900f”,“status”:“OK”,“status_str”:“正常”}, {“device_id”:“left-9667-4266-be34-d481032129fa”,“status”:“OK”,“status_str”:“正常”}, {“device_id”:“right-c27c-415d-bcb8-9ceb1e53d678”,“status”:“OK”,“status_str”:“正常”}, {“device_id”:“behind-88be-4424-a52a-c4ed74951448”,“status”:“OK”,“status_str”:“正常”}],“device_class”:“CAMERA”,“type”:“STATUS_REPORT”,“uuid”:""}
另外:
JsonFormat 中除了 nullAsEmtry 之外,还有 nullListAsEmpty,nullStringAsEmpty,nullBooleanAsFalse,nullNumberAsZero 等 遇到空值(null)的时候写入字符串的配置,有兴趣的同学可以依次验证
-----------------------------end by 2020-08-10-----------------------------