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


    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=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());
                }
            }
    
        }
    }
    

    输出结果

     

  • 相关阅读:
    计算机毕业设计springboot+vue+elementUI 广场舞团高校舞蹈社团管理系统
    前端数字计算精度问题
    如何实现一个优秀的 HashTable 散列表?
    2024年,提升Windows开发和使用体验实践 - 小工具篇
    数据结构第二课 -----线性表之顺序表
    java计算机毕业设计线上导医系统源码+数据库+系统+lw文档+mybatis+运行部署
    Word转PDF怎么转?学会这个方法,一键轻松搞定
    多线程---线程安全问题及解决
    【React】【react-globe.gl】3D Objects效果
    浅述青犀AI算法人体攀爬行为检测的应用场景及解决方案
  • 原文地址:https://blog.csdn.net/u012269637/article/details/126461924