- json字符串转jsonObject
JSONObject obj = com.alibaba.fastjson.JSON.parseObject(jsonStr);
- 将jsonObj转换成Map
Map<String, Object> map = JSONObject.toJavaObject(obj, Map.class);
- 将jsonMap字符串转换成Map
Map<String, Object> map = JSONObject.parseObject(jsonMap, Map.class);
- 将jsonList字符串转成List
List<Student> studentList = JSONObject.parseArray(jsonList,Student.class);
- 将Map转换成jsonObject
JSONObject obj = JSONObject.parseObject(com.alibaba.fastjson.JSON.toJSONString(map));
- 将List转换成JSONArray
JSONArray arr = JSONObject.parseArray(com.alibaba.fastjson.JSON.toJSONString(list));
示例:
{
"notifyType": "deviceDataChanged",
"deviceId": "21e2db44-7770-4eba-a455-48b76deb86d0",
"gatewayId": "21e2db44-7770-4eba-a455-48b76deb86d0",
"requestId": null,
"service": {
"serviceId": "PackLock",
"serviceType": "PackLock",
"data": {
"packinfo": "8986031749203866157386681803004919946011117474556199097902"
},
"eventTime": "20190423T025901Z"
}
}
Java代码:
JSONObject object = JSONObject.parseObject(jsonStr);
Map<String, Object> map = JSONObject.toJavaObject(object, Map.class);
System.out.println(map.get("notifyType"));