JAVA中JSON字符串的解析方法
需要解析的JSON
massage==>"{'deviceType':'CustomCategory','iotId':'LHGR0pgEEhI4FWZaIwAS000000','requestId':'123','checkFailedData':{},'productKey':'a17rRRuNTCD','gmtCreate':1607241627505,'deviceName':'aliServer','items':{'LightSwitch':{'value':1,'time':1607241627510},'CurrentTemperature':{'value':49,'time':1607241627510}}}"
多层解析
//下列主要解析的是items中的值
//content为jSON字符串或字符串
JSONObject object = JSONObject.fromObject(content);
//多层解析
JSONObject items = JSONObject.fromObject(object.getString("items"));
//再解析
JSONObject LightSwitchs = JSONObject.fromObject(items.getString("LightSwitch")
String value = LightSwitchs.getString("value");
Long time = LightSwitchs.getLong("time");
System.out.println(value+"-------------"+time);
单层解析
//单层解析
JSONObject object = JSONObject.fromObject(content);
String deviceType = object.getString("deviceType")
System.out.println(deviceType);