【问题标题】:Manage one object inside list of object java管理对象 java 列表中的一个对象
【发布时间】:2020-11-21 02:09:45
【问题描述】:

我有对象列表,该对象列表多次显示相同的对象,但所有重复对象的颜色字段不同。我想更改响应。

如果有人对此有解决方案,请创建通用方法并传递此列表并根据我的期望响应隐藏。

1.实际反应

[
    {
      "id": 21,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "America Phoenix",
      "city": "San Jose de Gracia",
      "eventImg": "Image Url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": true,
      "myFavourite": true,
      "color": "Yellow"
    },
    {
      "id": 21,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "America Phoenix",
      "city": "San Jose de Gracia",
      "eventImg": "Image Url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": true,
      "myFavourite": true,
      "color": "Green"
    },
    {
      "id": 76,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "string",
      "city": "Villa Juarez",
      "eventImg": "Imgage url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": false,
      "myFavourite": true,
      "color": "Red"
    },
    {
      "id": 76,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "string",
      "city": "Villa Juarez",
      "eventImg": "Imgage url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": false,
      "myFavourite": true,
      "color": "Black"
    }
]    

2.期待回应

[
    {
      "id": 21,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "America Phoenix",
      "city": "San Jose de Gracia",
      "eventImg": "Image Url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": true,
      "myFavourite": true,
      "eventType": [
            {
              "color": "Yellow"
            },
            {
              "color": "Green"
            }
      ]
    },
    {
      "id": 76,
      "startDate": "2020-07-21",
      "startTime": "10:28:34+05:30",
      "endDate": "2020-07-21",
      "endTime": "19:28:34+05:30",
      "eventName": "Stretegic Planing for Business Success",
      "location": "string",
      "city": "Villa Juarez",
      "eventImg": "Imgage url",
      "passport": false,
      "passportDiscountPercent": 5,
      "featured": false,
      "myFavourite": true,
      "eventType": [
            {
              "color": "Red"
            },
            {
              "color": "Black"
            }
      ]
    }   
]    

【问题讨论】:

    标签: java arrays json spring-boot


    【解决方案1】:

    第 1 步:创建架构类

    初始响应模型:

    public class InitialModel {
                String id;
                String color;
    }
    

    最终/转换后的响应模型:

    public class Model {
            String id;
            List<Color> eventType = new ArrayList<>();
    
            class Color {
                String color;
    
                public Color(String color) {
                    this.color = color;
                }
            }
        }
    

    第 2 步:迭代初始响应并将其转换为最终响应

    List<Model> modelList = new ArrayList<>();
    List<InitialModel> initialModelList = new Gson().fromJson(jsonString, new TypeToken<List<Model>>(){}.getType());
    
    for (InitialModel initialModel : initialModelList) {
    
    Model tempModel = null;
    
    // Check if model with same id already exists
    for(Model model : modelList){
        if(initialModel.id.equals(model.id)){
            tempModel = model;
            break;
        }
    }
    
    // Create new model if id doesn't exists
    if(tempModel==null) {
        tempModel = new Model();
        tempModel.id = initialModel.id;
        modelList.add(tempModel);
    }
    tempModel.eventType.add(new Color(initialModel.color));
    
    
    }
    

    结果 Json:

    String resultJson = new Gson().toJson(modelList);
    

    1.我已经添加了您的代码,但出现了一些错误

    private List<EventCustomDTO> commonListView(List<EventDtoClass> eventDtoClassList) {
            List<EventCustomDTO> eventCustomDTOList = new ArrayList<>();
            List<EventDtoClass> eventDtoClasses = new Gson().fromJson(eventDtoClassList.toString(), new TypeToken<List<EventCustomDTO>>(){}.getType());
            for (EventDtoClass eventDtoClass : eventDtoClasses) {
                EventCustomDTO eventCustomDTO = null;
                for(EventCustomDTO model : eventCustomDTOList){
                    if(eventDtoClass.getId().equals(model.getId())){
                        eventCustomDTO = model;
                        break;
                    }
                }
                if(eventCustomDTO == null) {
                    eventCustomDTO = new EventCustomDTO();
                    eventCustomDTO.getId() = eventDtoClass.getId();
                    eventCustomDTOList.add(eventCustomDTO);
                }
                eventCustomDTO.getEventType().add(new TypesOfEvents(eventDtoClass.getEvenTypeId(),eventDtoClass.getColor()));
            }
            return eventCustomDTOList;
        }
    

    【讨论】:

    • 如果已经找到模型,这不是添加两次吗?我认为你应该添加 modelList.add(tempModel);继续;到空检查?
    • jsonString 上放了什么?
    • jsonString 是 java 字符串格式的实际响应 JSON。
    • 在这一行中出现错误:eventCustomDTO.getId() = eventDtoClass.getId();预期变量
    • 我需要传递 List 而不是 jsonString
    【解决方案2】:

    由于您的预期结果中有不同的 JSON,因此您需要有 2 个类来序列化/反序列化。让我们考虑以下类:

    @Getter
    @AllArgsConstructor
    private class ObjectA {
        public Long id;
        public String color;
    }
    
    @ToString
    @AllArgsConstructor
    private class ObjectB {
        public Long id;
        public String eventType;
    }
    

    现在在这种情况下获得预期结果的代码将是这样的:

    List<ObjectA> object = List.of(new ObjectA(1L, "green"), new ObjectA(1L, "red"), new ObjectA(2L, "yellow"));
    object.stream().collect(Collectors.groupingBy(ObjectA::getId))
            .entrySet() // from map of grouped ObjectA's
            .stream()
            .map(entry -> new ObjectB(entry.getKey(), // GroupID
                                      entry.getValue() // list of grouped ObjectA's by current GroupID
                                             .stream()
                                             .map(ObjectA::getColor) // change to stream of all color strings from grouped ObjectA's by current GroupID
                                             .collect(Collectors.joining(", ")))
            ).forEach(System.out::println);
            //.collect(Collectors.toList());
    

    有输出:

    ObjectB(id=1, eventType=green, red)
    ObjectB(id=2, eventType=yellow)
    

    您可以根据您的用例简单地调整该代码。

    【讨论】:

      猜你喜欢
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      相关资源
      最近更新 更多