java测试嵌套对象的转换 String jsonData="{\"id\":null,\"createUser\":10017,\"createTime\":\"2022-08-16T09:18:08.000+08:00\",\"updateUser\":10017,\"updateTime\":\"2022-08-16T09:18:18.000+08:00\",\"tenantId\":0,\"dataVersion\":null,\"dataState\":\"1\",\"optContext\":null,\"flag\":1,\"pageModel\":null,\"pageMap\":null,\"permissionDeptId\":10001,\"permissionDeptIdList\":null,\"hasPermission\":true,\"filterPermission\":true,\"indexStdId\":2000,\"indexName\":\"测试指标\",\"enName\":\"ssss\",\"indexExpression\":\"jjj\",\"indexStatus\":3,\"remark\":null,\"indexStdFieldList\":[{\"id\":null,\"createUser\":10017,\"createTime\":\"2022-08-16T09:18:08.000+08:00\",\"updateUser\":10017,\"updateTime\":\"2022-08-16T09:18:08.000+08:00\",\"tenantId\":0,\"dataVersion\":null,\"dataState\":\"1\",\"optContext\":null,\"flag\":1,\"pageModel\":null,\"pageMap\":null,\"permissionDeptId\":10001,\"permissionDeptIdList\":null,\"hasPermission\":true,\"filterPermission\":true,\"indexStdFieldId\":82,\"indexStdId\":2000,\"fieldName\":\"kkk\",\"cnName\":\"kkk\",\"fieldDesc\":\"kkk\",\"orderNum\":1,\"remark\":null,\"newRecord\":true,\"valid\":true}],\"relatedDimensionId\":null,\"createUserName\":null,\"relatedDimensionCount\":null,\"newRecord\":true,\"valid\":true}"; JSONObject jsonObject = JSONObject.parseObject(jsonData); 转json数组 IndexStd indexStd = JSONObject.toJavaObject(jsonObject, IndexStd.class); 转java对象 System.out.println(indexStd.getIndexName());
第二种方式
package com.lgh.feixin.utils; import com.alibaba.fastjson.JSONObject; import com.lgh.feixin.model.Records; import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.List; /** * @Auther: LGH * @Date: 2022/12/13 09:08 * @Description: */ public class Text { public static void getMain() { String reoce="{\"totalSize\": 1,\n" + "\n" + " \"done\": true,\n" + "\n" + "\"nextRecordsUrl\":\"/services/data/v53.0/query/0r8BI14bO476SosYIE-1000\",\n" + "\n" + " \"records\": [\n" + "\n" + " {\n" + "\n" + " \"attributes\": {\n" + "\n" + " \"type\": \"Opportunity\",\n" + "\n" + " \"url\": \"/services/data/v56.0/sobjects/Opportunity/0060w00000ByjJPAAZ\"\n" + "\n" + " },\n" + "\n" + " \"History_Code__c\": \"P221000021\",\n" + "\n" + " \"Name\": \"测试1008\",\n" + "\n" + " \"Amount\": 6000000.0,\n" + "\n" + " \"StageName\": \"Purchase Order\",\n" + "\n" + " \"Project_Priority__c\": null,\n" + "\n" + " \"Equipment_Date__c\": null,\n" + "\n" + " \"DeviceActiveDate__c\": null,\n" + "\n" + " \"Robot_Mode1__c\": null,\n" + "\n" + " \"Robot_Amount1__c\": null,\n" + "\n" + " \"Rebotmode2__c\": null,\n" + "\n" + " \"RobotQuantity2__c\": null,\n" + "\n" + " \"Chargingpile_Quantity__c\": null,\n" + "\n" + " \"HAIPORTLoading__c\": null,\n" + "\n" + " \"HAIPORTUnloading__c\": null,\n" + "\n" + " \"HardwareLevel__c\": null,\n" + "\n" + " \"hardwareneeds__c\": null,\n" + "\n" + " \"Software_Level__c\": null,\n" + "\n" + " \"SoftwareType__c\": null,\n" + "\n" + " \"Project_Country__c\": \"a010w000006iHVjAAM\",\n" + "\n" + " \"Region__c\": \"Japan\",\n" + "\n" + " \"ProjectLocationCity__c\": null,\n" + "\n" + " \"WarehouseType__c\": \"2B2C发货仓\",\n" + "\n" + " \"OwnerId\": \"0055j000005BEuWAAW\",\n" + "\n" + " \"Solution__c\": null,\n" + "\n" + " \"ContractDate1__c\": null,\n" + "\n" + " \"Percent80_date__c\": \"2022-11-03\",\n" + "\n" + " \"ZeroIndustry1__c\": null,\n" + "\n" + " \"PimaryIndustry__c\": null,\n" + "\n" + " \"CreateDate__c\": \"2022-10-11T08:51:57.000+0000\",\n" + "\n" + " \"Integrators__c\": null,\n" + "\n" + " \"Important_Projects__c\": null,\n" + "\n" + " \"History_Id__c\": null\n" + "\n" + " }\n" + "\n" + " ]}"; List<Records> records=new ArrayList<>(); JSONObject jsonBody = JSONObject.parseObject(reoce); String strRecords = jsonBody.getString("records"); if (StringUtils.isNotEmpty(strRecords)) { records = JSONObject.parseArray(strRecords, Records.class); } if (!CollectionUtils.isEmpty(records)){ for (Records records1:records){ System.out.println(records1.getAttributes().get(0).getUrl()); } } } }
输出结果