**json字符串:{"name":"JSON","age":"24","address":"北京市西城区"} 和 map 展示是一样的** 虽然看格式是json 对象但是实际传的时候也得 String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}"; 在对象中两边价格“”
private static Map<String,Object> changeStr1() {
//切记:json 字符串时要加 "" 如何{"name":"JSON","age":"24","address":"北京市西城区"} 这个变为下面objectStr 只要在java 页面中把{"name":"JSON","age":"24","address":"北京市西城区"} 放到 String objectStr="" ""中就行!!!!!
String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}";
JSONObject jsonObject = JSONObject.parseObject(objectStr);
//因为和map 一样都是key value 形式所以可以直接用map 来接收
**Map<String, Object> jsonObject1 = jsonObject;**
System.out.println(jsonObject1); //输出: {"address":"北京市西城区","name":"JSON","age":"24"}
return jsonObject1;
}
public static void main(String[] args) {
TestController.changeStr1();
}
参数:
[
{"checkId": "45b3b4b2-4779-8b36-8c0c-a06f4e0ce25d",
"principal": "",
"principalId": "彭修亮",
"problemId": "8273ffe4-9e20-1d1e-8826-c35a7687917f",
"problemItem": "1",
"rectificationDate": "2019-08-06T17:29:58"
},
{"checkId": "5b3b4b2-4779-8b36-8c0c-a06f4e0ce25d",
"principal": "",
"principalId": "彭修亮",
"problemId": "273ffe4-9e20-1d1e-8826-c35a7687917f",
"problemItem": "1",
"rectificationDate": "2019-08-06T17:29:58"
}
]
但是传参数也可以是 String data
@Override
@Transactional
public void batchSave(String data) throws Exception {
try{
**List<StRiskCraneAppearance> listData = JSONArray.parseArray(data,StRiskCraneAppearance.class);**
for(StRiskCraneAppearance content : listData){
this.repository.save(content);
}
}catch (Exception e){
e.printStackTrace();
}
}
另一种情况:
参数:
{
“inspectType”:[
inspectTypeId": "",//主键检查人ID
inspectId//检查ID
inspectTypeCode//检查类别
],
“inspectContent”:[
inspectContentId//主键
inspectTypeId//检查人ID
contentCode//检查内容
],
“sign”:[
signId//主键
businessId ://inspectTypeId//检查人ID
confirmorId//确认人ID
confirmor//确认人
confirmorSign//确认人签字
confirmorSignTime//确认人签字时间
]
}
但是传参数也可以是 String data 这类参数是
@Override
@Transactional
public void batchSave(String data){
try{
**JSONObject jsonObject = JSON.parseObject(data);**
//检查人
List<StRiskFireInspectType> inspectTypeList = JSONArray.parseArray(jsonObject.get("inspectType").toString(),StRiskFireInspectType.class);
for(StRiskFireInspectType type : inspectTypeList){
inspectTypeRepository.save(type);
}
//签字
List<StRiskSign> signList = JSONArray.parseArray(jsonObject.get("sign").toString(),StRiskSign.class);
for(StRiskSign stRiskSign : signList){
stRiskSignRepository.save(stRiskSign);
}
//检测内容,存储前先删除已经存在的信息
List<StRiskFireInspectContent> contentList = JSONArray.parseArray(jsonObject.get("inspectContent").toString(),StRiskFireInspectContent.class);
try{
for(StRiskFireInspectType type : inspectTypeList){
inspectContentRepository.deleteByInspectTypeId(type.getInspectTypeId());
}
}catch (Exception e){}
for(StRiskFireInspectContent content : contentList){
inspectContentRepository.save(content);
}
}catch (Exception e){
e.printStackTrace();
}
}
放入json 字符串后是这样的:
private static String changeStr1() {
// String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}";
String data="{\n" +
" \"resultCode\": 0,\n" +
" \"result\": {\n" +
" \"extField\": null,\n" +
" \"permitId\": \"557140bf3ab4253deea3168086684edc\",\n" +
" \"versionId\": \"VpjwGCg41y\",\n" +
" \"jobId\": \"o478ptsanU\",\n" +
" \"siteId\": \"10040027\",\n" +
" \"permitType\": \"DigWork\",\n" +
" \"parentPermitId\": null,\n" +
" \"permitNumber\": \"WJZY-201908000004\",\n" +
" \"applicantUnitId\": null,\n" +
" \"applicantUnit\": null,\n" +
" \"workUnitId\": null,\n" +
" \"workUnitName\": null,\n" +
" \"workArea\": null,\n" +
" \"workPosition\": null,\n" +
" \"workContent\": \"独山子作业区\",\n" +
" \"startDate\": null,\n" +
" \"endDate\": null,\n" +
" \"safetyPrecautions\": null,\n" +
" \"isGas\": null,\n" +
" \"gasInterval\": null,\n" +
" \"gasRequirement\": null,\n" +
" \"applicantId\": \"123214\",\n" +
" \"applicant\": \"管理员\",\n" +
" \"applyTime\": \"2019-08-20T17:50:01\",\n" +
" \"approveTime\": null,\n" +
" \"closeTime\": null,\n" +
" \"cancelRemark\": null,\n" +
" \"cancelTime\": null,\n" +
" \"useStatus\": \"WaitingCommit\",\n" +
" \"isHaveParties\": null,\n" +
" \"affectedParties\": null,\n" +
" \"isAnnex\": null,\n" +
" \"annexDesc\": null,\n" +
" \"longitude\": null,\n" +
" \"latitude\": null,\n" +
" \"rejectorId\": null,\n" +
" \"rejector\": null,\n" +
" \"rejectTime\": null,\n" +
" \"rejectOpinion\": null,\n" +
" \"processId\": null,\n" +
" \"version\": {\n" +
" \"createDate\": \"2018-12-03T00:00:00\",\n" +
" \"creatorId\": \"bayin.nj\",\n" +
" \"creatorName\": \"巴音\",\n" +
" \"updateDate\": \"2018-12-03T00:00:00\",\n" +
" \"updatorId\": \"bayin.nj\",\n" +
" \"updatorName\": \"巴音\",\n" +
" \"versionId\": \"VpjwGCg41y\",\n" +
" \"siteId\": null,\n" +
" \"permitType\": \"DigWork\",\n" +
" \"version\": \"2\",\n" +
" \"versionYear\": 2018,\n" +
" \"startDate\": \"2018-11-29\",\n" +
" \"useStatus\": 1,\n" +
" \"extField\": null,\n" +
" \"deleteFlag\": \"0\",\n" +
" \"lockFlag\": \"0\",\n" +
" \"remark\": null,\n" +
" \"permitTypeObject\": {\n" +
" \"createDate\": \"2018-11-08T00:00:00\",\n" +
" \"creatorId\": \"gujf.nj\",\n" +
" \"creatorName\": \"顾俊峰\",\n" +
" \"updateDate\": \"2019-06-25T12:07:40\",\n" +
" \"updatorId\": \"admin.nj\",\n" +
" \"updatorName\": \"管理员\",\n" +
" \"codeId\": \"BEdpk2v8qk\",\n" +
" \"code\": \"DigWork\",\n" +
" \"parentCode\": \"PermitType\",\n" +
" \"codePath\": \"StRisk.PermitType.DigWork\",\n" +
" \"codeName\": \"挖掘作业许可证\",\n" +
" \"codeNamePath\": \"高危作业.许可证类型.挖掘作业许可证\",\n" +
" \"codeParentId\": \"Q5qnX0OyZn\",\n" +
" \"orderNum\": \"000700030007\",\n" +
" \"comments\": null,\n" +
" \"isLocked\": false,\n" +
" \"isValid\": true,\n" +
" \"isDeleted\": false,\n" +
" \"extend\": \"1\",\n" +
" \"extend2\": null\n" +
" },\n" +
" \"permitTypeName\": \"挖掘作业许可证\"\n" +
" },\n" +
" \"job\": {\n" +
" \"createDate\": \"2019-07-15T00:00:00\",\n" +
" \"creatorId\": null,\n" +
" \"creatorName\": null,\n" +
" \"updateDate\": \"2019-08-17T20:27:12\",\n" +
" \"updatorId\": \"admin.nj\",\n" +
" \"updatorName\": \"管理员\",\n" +
" \"jobId\": \"o478ptsanU\",\n" +
" \"siteId\": \"10040027\",\n" +
" \"jobName\": \"轴流风机月度维护保养\",\n" +
" \"jobContent\": \"独山子作业区\",\n" +
" \"jobArea\": \"\\n\\n独山子作业区\",\n" +
" \"productionDept\": \"\\n独山子作业区\",\n" +
" \"riskType\": null,\n" +
" \"applicantId\": null,\n" +
" \"applicant\": null,\n" +
" \"applyTime\": null,\n" +
" \"registrationTime\": \"2019-07-15T00:00:00\",\n" +
" \"isMaintenance\": null,\n" +
" \"useStatus\": null,\n" +
" \"extField\": null,\n" +
" \"deleteFlag\": null,\n" +
" \"lockFlag\": null,\n" +
" \"remark\": null,\n" +
" \"productionDeptId\": null,\n" +
" \"jobAreaId\": null,\n" +
" \"jobDeptId\": null,\n" +
" \"jobDept\": \"独山子作业区\",\n" +
" \"longitude\": null,\n" +
" \"latitude\": null,\n" +
" \"processId\": null,\n" +
" \"jobInfo\": null\n" +
" },\n" +
" \"useStatusName\": \"待提交\",\n" +
" \"permitTypeName\": \"挖掘作业许可证\",\n" +
" \"work\": null,\n" +
" \"fire\": null,\n" +
" \"space\": null,\n" +
" \"open\": null,\n" +
" \"electricity\": null,\n" +
" \"hoisting\": null,\n" +
" \"dig\": {\n" +
" \"permitId\": \"557140bf3ab4253deea3168086684edc\",\n" +
" \"diggingReason\": null,\n" +
" \"drawing\": null,\n" +
" \"isDrawing\": null,\n" +
" \"drawingDesc\": null,\n" +
" \"isWorkingPlan\": null,\n" +
" \"otherRisk\": null,\n" +
" \"operatorId\": null,\n" +
" \"participants\": \"钟有金,湛胜腾\"\n" +
" },\n" +
" \"high\": null,\n" +
" \"photo\": null,\n" +
" \"ray\": null,\n" +
" \"vehicle\": null,\n" +
" \"participant\": {\n" +
" \"participantId\": \"MwSFHRh1Je1fxopkWB2XqqlzfvtTLsgX,AnKvvSmO8D6WHGUlJWefJL9QStBfBrX0,XaPchmTV39WOPRjAUhKg8XTsn2auN6v\",\n" +
" \"participant\": \"湛胜腾,钟有金,\"\n" +
" },\n" +
" \"applicantDept\": null,\n" +
" \"configs\": [],\n" +
" \"signs\": [],\n" +
" \"delays\": [],\n" +
" \"wpJob\": {\n" +
" \"jobId\": \"o478ptsanU\",\n" +
" \"itemId\": \"jxstar-201712000937\",\n" +
" \"jobStartDate\": \"2019-05-24T00:00:00.000\",\n" +
" \"jobPlanEndDate\": \"2019-06-03T00:00:00.000\",\n" +
" \"jobActualEndDate\": null,\n" +
" \"locationId\": \"2435\",\n" +
" \"locationName\": \"独乌成10#阀室\",\n" +
" \"riskLevel\": \"RiskWorkLevelHigh\",\n" +
" \"principalUserId\": null,\n" +
" \"principalUserName\": \"杨家玮\",\n" +
" \"jobType\": \"1\",\n" +
" \"isNeedJsa\": \"1\",\n" +
" \"isNeedTechnicalDisclosure\": \"1\",\n" +
" \"extField\": null,\n" +
" \"deleteFlag\": \"0\",\n" +
" \"lockFlag\": \"0\",\n" +
" \"createDate\": null,\n" +
" \"creatorId\": null,\n" +
" \"creatorName\": null,\n" +
" \"updateDate\": \"2019-08-17T20:27:12.000\",\n" +
" \"updatorId\": \"admin.nj\",\n" +
" \"updatorName\": \"管理员\",\n" +
" \"remark\": \"dd\",\n" +
" \"siteId\": \"10040027\",\n" +
" \"branchDeptId\": null,\n" +
" \"branchDeptName\": null,\n" +
" \"executionDeptId\": null,\n" +
" \"executionDeptName\": \"独山子作业区\",\n" +
" \"managementDeptId\": null,\n" +
" \"managementDeptName\": \"独山子作业区\",\n" +
" \"recordState\": \"recordState5\",\n" +
" \"workOrderState\": \"workOrderState1\",\n" +
" \"workOrderNum\": null,\n" +
" \"realWorkHour\": 4,\n" +
" \"auditWorkHour\": null,\n" +
" \"createJobProcessId\": null,\n" +
" \"closeJobProcessId\": \"qhijeksq4rvrenkpy7ll9qbw\",\n" +
" \"itemName\": \"轴流风机月度维护保养\",\n" +
" \"itemCode\": \"201712000937\"\n" +
" },\n" +
" \"commit\": false\n" +
" },\n" +
" \"msgId\": null,\n" +
" \"success\": true\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(data);
JSONObject wpJob = (JSONObject) jsonObject.get("result");
JSONObject wpJob1 = (JSONObject) wpJob.get("wpJob");
String itemName = (String) wpJob1.get("itemName");
// Map<String, Object> jsonObject1 = jsonObject;
System.out.println(itemName); //输出: 轴流风机月度维护保养
return itemName;
}