在实际开发中 遇到较复杂的json对象 如下:
function sendJson() {
var stu={
"id": "sdfs",
"orgId": "sdfsdf",
"schemeId": "sdfs",
"executeTime": "20180223T122363",
"userName": "",
"schemeXML": "SDFSDFSDFSDF",
"isManual": true,
"runningStatus": 0,
"listContent": [
{
"relationId": "sdfs ",
"des1": "232",
"des2": "23",
"analysisResult": "232",
"ListCameraResult": [
{
"cameraId": "23",
"guideRailId": "23",
"pictPath": "3232",
"recordFileSessiondId": "wqeqw",
"presetIndex": "2",
"guidePresetIndex": "23",
"resultGenTime": "21321321",
"analysisType": "2",
"para1": "1",
"para2": "2",
"para3": "3",
"para4": "4",
"para5": "5",
"para6": "6",
"para7": "7",
"para8": "8"
}
],
"ListEleDeviceResults": [
{
"id": "11",
"ListCamera": [
{
"presetIndex": "23",
"guidePresetIndex": "232",
"cameraId": "232",
"guideRailId": "232",
"pictPath": "drwqrq"
}
],
"eleId": "23",
"resultGenTime": "232",
"pictPath": "232",
"analysisType": "323",
"para1": "2332",
"para2": "",
"para3": "",
"para4": "",
"para5": "",
"para6": "",
"para7": "",
"para8": ""
}
]
}
]
};
可以发现 这是一个json对象里面有多个json数组 json数组里面在套数组的格式
java后台使用框架为Jfianl框架
第一种方式:
/* 获取到前端传递json*/
String JsonArr = HttpKit.readData(getRequest());
ReqPatrolSchemeInfoModel rModel = JSON.parseObject(JsonArr, ReqPatrolSchemeInfoModel.class);
(所需jar包:com.alibaba.fastjson.JSON)
当然java bean ReqPatrolSchemeInfoModel 必须和前端传入json格式保持一致
省略get set..
第二种方式:
使用google提供的
Gson gson = new Gson();
ReqPatrolSchemeInfoModel person = gson.fromJson(JsonArr,ReqPatrolSchemeInfoModel.class);
第三种方式:
/* 转为json对象*/
// JSONObject jObject = JSONObject.fromObject(JsonArr);
// ReqPatrolSchemeInfoModel rModel =(ReqPatrolSchemeInfoModel)jObject.toBean(jObject,ReqPatrolSchemeInfoModel.class);
/* jsonObkect转java对象*/
// Map
// classMap.put("listContentClass", ListContent.class);
// classMap.put("ListCameraResult", CameraResults.class);
// classMap.put("listEleDeviceResults", EleDeviceResults.class);
// classMap.put("list", ListCamera.class);
// ReqPatrolSchemeInfoModel rModel =(ReqPatrolSchemeInfoModel)JSONObject.toBean(jObject,ReqPatrolSchemeInfoModel.class,classMap);
// System.out.println(rModel);