JSONObject.toJSONString()自动修改属性格式,改变了大小写

在接口对接中,使用FastJSON序列化List对象时,发现属性名大小写被更改,影响客户端数据接收。通过在POJO类get方法上添加@JSONField注解,成功保留属性名原始格式。

       在一次接口对接的过程中,本想把查询到的list对象通过fastjson序列化后传给客户端

客户端需要的数据格式:

[

  {

    "RefID": "4365665",

    "Title": "任务标题",

    "Url": "http://10.10.1.131/oa/flow/?id=kfi921d9",

    "Receiver": "kf9654126",

    "ReceiveDate": 1505976952

  },

  {

    "RefID": "4365665",

    "Title": "任务标题",

    "Url": "http://10.10.1.131/oa/flow/?id=kfi921d9",

    "Receiver": "kf9654126",

    "ReceiveDate": 1505976952

  }

]

然而这些属性通过JSONObject.toJSONString序列化后变成了

 {
        "receiveDate": ,
        "receiver": "",
        "refID": "",
        "title": "",
        "url": ""
   },

找了下,说是需要在pojo类的各个属性get方法上配置@JSONField(name="xxx")

需注意,不是在属性上标该注解,是在get方法很多文章有误导

这样设置后返回客户端的属性的大小写就不会被修改了

@Override public ResponseDto getDepartmentTreeList(Integer departmentId,Integer isPeople) { Integer rootOrg; if (Objects.isNull(departmentId)) { rootOrg = ConfigUtil.getInt(ConfigUtil.ROOT_TISSUE); } else { rootOrg = departmentId; } String redisKey = ObjectUtil.join(Contacts.SYNTHESIZE_DEPART_TREE_CACHE,rootOrg,":",isPeople); if (RedisUtil.exists(redisKey)) { String dataStr = RedisUtil.get(redisKey); return ResponseDto.success(JSONArray.parseArray(dataStr)); } final DepartDto[] dto = new DepartDto[1]; List<DepartDto> departmentList = noticeMapper.getDepartmentList(null); Map<Integer, List<DepartDto>> collect = departmentList.stream().collect(Collectors.groupingBy(DepartDto::getParentid)); departmentList.forEach(e -> { if (Objects.equals(rootOrg, e.getId())) { dto[0] = e; } e.setChildNode(collect.get(e.getId())); }); List<DepartDto> collect1 = departmentList.stream().filter(e -> Objects.equals(rootOrg, e.getParentid())).collect(Collectors.toList()); if (isPeople == 1) { dto[0].setChildNode(collect1); RedisUtil.set(redisKey,JSONArray.toJSONString(dto),1,TimeUnit.DAYS); return ResponseDto.success(dto); } CountDownLatch countDownLatch = new CountDownLatch(collect1.size()); JSONArray childNodes = new JSONArray(); JSONObject deptDto = new JSONObject(); try { collect1.forEach(departDto-> executor.execute(()->{ JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(departDto)); if (Assert.isNotEmpty(departDto.getChildNode())) { JSONArray leader = getChildNodeLeaderList(departDto.getId()); JSONArray childNode = object.getJSONArray("childNode"); leader.addAll(childNode); object.put("childNode",leader); } childNodes.add(object); countDownLatch.countDown(); })); countDownLatch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } JSONArray leader = getChildNodeLeaderList(rootOrg); leader.addAll(childNodes); deptDto.putAll(JSONObject.parseObject(JSONObject.toJSONString(dto[0]))); deptDto.put("childNode",leader); JSONArray result = new JSONArray(); result.add(deptDto); RedisUtil.set(redisKey,JSONArray.toJSONString(result),1,TimeUnit.DAYS); return ResponseDto.success(result); }解释代码
最新发布
09-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值