如何将JSON字符串里面的某些的json字符串格式转成json对象?

背景

下游传过来的数据是一个json字符串,这个json字符串里面有的字段又套着json字符串!还有一些字段直接是null传过来的!现在要去掉null,且将一些json字符串!尽可能的换成json对象!

如何将JSON字符串里面的某些的json字符串格式转成json对象? 升级版

{
  "id": null,
  "type": null,
  "content": "{\"marketCardId\":null,\"name\":\"最后创建一下\",\"type\":null,\"guideWords\":null,\"cardTitle\":\"文本+链接试一下12\",\"rightsExplain\":[\"测试2\"],\"smsVerificationType\":0,\"contactInfo\":null,\"productInfo\":null,\"jumpUrlInfo\":\"https://weibo.com/at/weibo\",\"acqLinkId\":null,\"acqLinkName\":null,\"acqLinkGroupId\":null,\"acqLinkGroupName\":null,\"content\":\"{\\\"marketCardId\\\":null,\\\"name\\\":\\\"最后创建一下\\\",\\\"type\\\":null,\\\"guideWords\\\":null,\\\"cardTitle\\\":\\\"文本+链接试一下12\\\",\\\"rightsExplain\\\":[\\\"测试2\\\"],\\\"smsVerificationType\\\":0,\\\"contactInfo\\\":null,\\\"productInfo\\\":null,\\\"jumpUrlInfo\\\":\\\"https://weibo.com/at/weibo\\\",\\\"acqLinkId\\\":null,\\\"acqLinkName\\\":null,\\\"acqLinkGroupId\\\":null,\\\"acqLinkGroupName\\\":null,\\\"acqLinkType\\\":null,\\\"createTime\\\":null,\\\"cardSourceFrom\\\":0,\\\"headUrl\\\":null}\",\"createTime\":null,\"cardSourceFrom\":0,\"headUrl\":null}",
  "configAuditStatusEnum": null
}

临时方案

  • 使用com.alibaba.fastjson.JSONObject
  • 使用深度遍历
    @Test
    public void buildMvelScriptBusinessPrivateMsgAudit() {
        String content = "{\"id\":null,\"merchantId\":null,\"type\":null,\"content\":\"{\\\"marketCardId\\\":null,\\\"name\\\":\\\"最后创建一下\\\",\\\"type\\\":null,\\\"guideWords\\\":null,\\\"cardTitle\\\":\\\"文本+链接试一下12\\\",\\\"rightsExplain\\\":[\\\"测试2\\\"],\\\"smsVerificationType\\\":0,\\\"contactInfo\\\":null,\\\"productInfo\\\":null,\\\"jumpUrlInfo\\\":\\\"https://weibo.com/at/weibo\\\",\\\"acqLinkId\\\":null,\\\"acqLinkName\\\":null,\\\"acqLinkGroupId\\\":null,\\\"acqLinkGroupName\\\":null,\\\"content\\\":\\\"{\\\\\\\"marketCardId\\\\\\\":null,\\\\\\\"name\\\\\\\":\\\\\\\"最后创建一下\\\\\\\",\\\\\\\"type\\\\\\\":null,\\\\\\\"guideWords\\\\\\\":null,\\\\\\\"cardTitle\\\\\\\":\\\\\\\"文本+链接试一下12\\\\\\\",\\\\\\\"rightsExplain\\\\\\\":[\\\\\\\"测试2\\\\\\\"],\\\\\\\"smsVerificationType\\\\\\\":0,\\\\\\\"contactInfo\\\\\\\":null,\\\\\\\"productInfo\\\\\\\":null,\\\\\\\"jumpUrlInfo\\\\\\\":\\\\\\\"https://weibo.com/at/weibo\\\\\\\",\\\\\\\"acqLinkId\\\\\\\":null,\\\\\\\"acqLinkName\\\\\\\":null,\\\\\\\"acqLinkGroupId\\\\\\\":null,\\\\\\\"acqLinkGroupName\\\\\\\":null,\\\\\\\"acqLinkType\\\\\\\":null,\\\\\\\"createTime\\\\\\\":null,\\\\\\\"cardSourceFrom\\\\\\\":0,\\\\\\\"headUrl\\\\\\\":null}\\\",\\\"createTime\\\":null,\\\"cardSourceFrom\\\":0,\\\"headUrl\\\":null}\",\"extendUnitList\":[{\"id\":null,\"merchantId\":null,\"type\":null,\"content\":\"{\\\"id\\\":null,\\\"switchStatus\\\":null,\\\"type\\\":null,\\\"questionTitle\\\":null,\\\"content\\\":\\\"\\\"}\",\"isDel\":null,\"createTime\":null,\"updateTime\":null}],\"auditSeqId\":null,\"configAuditStatusEnum\":null}";

        Set<String> jsonKeys = new HashSet<>(Arrays.asList("content","extendUnitList"));
        Map<String, Object> jsonMap = transformJson(content, jsonKeys);
        System.out.println("jsonMap=" + JSONObject.toJSONString(jsonMap));
    }

	private Map<String, Object> transformJson(String content, Set<String> jsonKeys) {
        Map<String, Object> contentMapNew = new HashMap<>();
        if (JSONObject.isValid(content)) {
            Map<String, Object> contentMap = JSONObject.parseObject(content, HashMap.class);
            contentMap = contentMap.entrySet().stream()
                    .filter(entry -> Objects.nonNull(entry.getValue()))
                    .filter(entry -> !Objects.equals("null", entry.getValue()))
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            Map.Entry::getValue,
                            (oldValue, newValue) -> oldValue,
                            HashMap::new));
            for (Entry<String, Object> entry : contentMap.entrySet()) {
                if (jsonKeys.contains(entry.getKey())
                        && entry.getValue() instanceof String
                        && JSONObject.isValid((String) entry.getValue())) {
                    contentMapNew.put(entry.getKey(), transformJson((String) entry.getValue(), jsonKeys));
                } else if (jsonKeys.contains(entry.getKey())
                        && entry.getValue() instanceof List
                        && ((List) entry.getValue()).size() > 0) {
                    List ls = new ArrayList<>();
                    for (Object o : ((List) entry.getValue())) {
                        if (o instanceof String) {
                            ls.add(transformJson((String) o, jsonKeys));
                        } else if (o instanceof Map) {
                            ls.add(transformJson(JSONObject.toJSONString(o), jsonKeys));
                        }
                    }
                    contentMapNew.put(entry.getKey(), ls);
                } else {
                    contentMapNew.put(entry.getKey(), entry.getValue());
                }
            }
        }
        return contentMapNew;
    }

感兴趣的同学还可以扩展!这里只是一个简单思路!

最好的方案

让上游给传正确的格式!!!

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值