【问题标题】:In one JSON object one field has same value but rest of fields has different values , need same value as common rest of values as list of items在一个 JSON 对象中,一个字段具有相同的值,但其余字段具有不同的值,需要与作为项目列表的常见其余值相同的值
【发布时间】:2021-09-09 09:22:51
【问题描述】:

我按资源名称对项目列表进行分组,我得到如下输出,即在每个 JSON 对象中 resourceName 是相同的,其他字段值不同,但我需要资源名称应该作为公共值其余的都是值列表。


{"groupedResourceDtos": {
    "Resource PopCheck": {
        "9": [{
            "id": 9,
            "resourceName": "Resource PopCheck",
            "groupedMonths": {
                "id": 9,
                "monthName": "Aug",
                "noOfLicenses": 8
            }
        }],
        "10": [{
            "id": 10,
            "resourceName": "Resource PopCheck",
            "groupedMonths": {
                "id": 10,
                "monthName": "Sep",
                "noOfLicenses": 11
            }
        }],
        "11": [{
            "id": 11,
            "resourceName": "Resource PopCheck",
            "groupedMonths": {
                "id": 11,
                "monthName": "Oct",
                "noOfLicenses": 7
            }
        }]
    }
}    

但我期待

{"groupedResourceDtos": {
    "resourceName":  "Resource PopCheck",
    "groupedMonths": [{
        "id": 9,
        "monthName": "Aug",
        "noOfLicenses": 8
    },{
        "id": 10,
        "monthName": "Sep",
        "noOfLicenses": 11
    },{
        "id": 11,
        "monthName": "Oct",
        "noOfLicenses": 7
    }]
}}

请帮我解决这个问题

【问题讨论】:

  • 欢迎来到 SO。请阅读How to Ask 并显示一些代码,即到目前为止您尝试了什么?另请注意,如果没有看到它映射到的对象以及要用于生成所需输出的代码,json 数据并没有太大帮助。另外请重新格式化您的帖子以使其更具可读性(例如修复缩进)并详细说明您需要的输出应该如何构建以及来自什么输入。
  • 预期的 JSON 无效,请查看并提供正确的 JSON 示例或更好的 POJO。
  • @AlexRudenko 我提供了正确的 JSON 请检查​​一次,在此先感谢
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: java spring java-8


【解决方案1】:

首先,需要为提到的 JSON 定义 DTO 类。 这可以按如下方式完成(使用 Lombok 注释):

@Data
static class Dto {
    private Map<String, Map<String, List<MyResource>>> groupedResourceDtos;
}

@Data
static class MyResource {
    int id;
    String resourceName;
    GroupedMonth groupedMonths;
}

@Data
static class GroupedMonth {
    int id;
    String monthName;
    int noOfLicenses;
}

那么预期输出的 DTO 应该如下所示:

@Data
@AllArgsConstructor
static class Remap {
    private String resourceName;
    private List<GroupedMonth> groupedMonths;
}

假设存在使用 Jackson ObjectMapper 从输入 JSON 解析的 Dto 类的有效实例,则可以使用应用于映射内容的 Stream::flatMapStream::map 操作创建重新映射的实例:

String json = "{...};" // large input JSON 

ObjectMapper mapper = new ObjectMapper();

Dto dto = mapper.readValue(json, Dto.class);

Remap remap = dto.groupedResourceDtos.entrySet()
   .stream()
   .map(e -> new Remap(
        e.getKey(), // resourceName
        (e.getValue().values()      // Collection<List<MyResource>>
            .stream()               // Stream<List<MyResource>>
            .flatMap(List::stream)  // Stream<MyResource>
            .map(MyResource::getGroupedMonths) // Stream<GroupedMonth>
            .collect(Collectors.toList())
        ) // List<GroupedMonth>
   ))
   .findFirst() // Optional<Remap>, dealing with only 1 value
   .orElseThrow(IllegalArgumentException::new);

那么remap的值可能会被序列化为JSON:

System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(remap));

输出:

{
  "resourceName" : "Resource PopCheck",
  "groupedMonths" : [ {
    "id" : 9,
    "monthName" : "Aug",
    "noOfLicenses" : 8
  }, {
    "id" : 10,
    "monthName" : "Sep",
    "noOfLicenses" : 11
  }, {
    "id" : 11,
    "monthName" : "Oct",
    "noOfLicenses" : 7
  } ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2012-11-18
    相关资源
    最近更新 更多