900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > JSONObject将json字符串转成java嵌套对象

JSONObject将json字符串转成java嵌套对象

时间:2019-02-21 08:21:58

相关推荐

JSONObject将json字符串转成java嵌套对象

java测试嵌套对象的转换String jsonData="{\"id\":null,\"createUser\":10017,\"createTime\":\"-08-16T09:18:08.000+08:00\",\"updateUser\":10017,\"updateTime\":\"-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\":\"-08-16T09:18:08.000+08:00\",\"updateUser\":10017,\"updateTime\":\"-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 mons.lang3.StringUtils;import org.springframework.util.CollectionUtils;import java.util.ArrayList;import java.util.List;/*** @Auther: LGH* @Date: /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\": \"-11-03\",\n" +"\n" +" \"ZeroIndustry1__c\": null,\n" +"\n" +" \"PimaryIndustry__c\": null,\n" +"\n" +" \"CreateDate__c\": \"-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());}}}}

输出结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。