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

    }
}

输出结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值