当前位置: 首页 > 知识库问答 >
问题:

按JSON数组的唯一ID分组

钱运浩
2023-03-14

块引号

[
    {
        "ID": "1234",
        "CustomerName": "KUMAR",
        "BranchName": "HARBOUR",
        "SchemeName": "GOLD",
        "MobileNumber": "123456789",
        "CustomerType": "PRIMARY",
        "DedupeFound" : "NO"
        
    },
    {
        "ID": "1234",
        "CustomerName": "SEAN",
        "BranchName": "HARBOUR",
        "SchemeName": "GOLD",
        "MobileNumber": "123456789",
        "CustomerType": "SECONDARY",
        "DedupeFound" : "YES"
    },
    {
        "ID": "5678",
        "CustomerName": "MARK",
        "BranchName": "CANTONMENT",
        "SchemeName": "DIAMOND",
        "MobileNumber": "123456789",
        "CustomerType": "PRIMARY",
        "DedupeFound" : "NO"
    },
    {
        "ID": "5678",
        "CustomerName": "STEVE",
        "BranchName": "CANTONMENT",
        "SchemeName": "DIAMOND",
        "MobileNumber": "123456789",
        "CustomerType": "SECONDARY",
        "DedupeFound" : "YES"
    }
]
    null
[
    {
        "ID": "1234",
        "CustomerName": "KUMAR",           // Only Primary Customer Details for the 
        "BranchName": "HARBOUR",           //  ID Tag to be displayed here
        "SchemeName": "GOLD",
        "MobileNumber": "123456789"
        "DedupeDetails": [
            {
                "CustomerType": "PRIMARY"
                "CustomerName": "KUMAR",
                "DedupeFound" : "NO"
            },
            {
                "CustomerType": "SECONDARY"
                "CustomerName": "SEAN",
                "DedupeFound" : "YES"
            }
        ]
    },
    {
        "ID": "5678",
        "CustomerName": "MARK",
        "BranchName": "CANTONMENT",
        "SchemeName": "DIAMOND",
        "MobileNumber": "123456789"
        "DedupeDetails": [
            {
                "CustomerType": "PRIMARY"
                "CustomerName": "MARK",
                "DedupeFound" : "NO"
            },
            {
                "CustomerType": "SECONDARY"
                "CustomerName": "STEVE",
                "DedupeFound" : "YES"
            }
        ]
    }
]

爪哇

package com.mycompany.Login;

import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.sql.Timestamp;
import java.io.File;
import java.io.PrintWriter;
import java.sql.Time;
import com.mycompany.Login.*;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycompany.Dedupe.DedupeRoot.DedupeRes;




public class LoginMapping implements Processor{
    public void process(Exchange ex)throws Exception{ 
    
        try {   
        
           
            String responseString = {Input mentioned in post};          
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);         
            List<DedupeRes> dedupe = Arrays.asList(mapper.readValue(responseString, DedupeRes[].class));  
            int total = dedupe.size();          
            if (total > 0)
            {               
                for (int i = 0; i < total; i++) {                   
                    
                }               
            }
    
     ex.getIn().setBody(responseString);
        }       
        catch(Exception e) {
            ex.getIn().setHeader("ExpMsg", "Undefined");
            throw e;
        }   
    
}
}

共有1个答案

经兴安
2023-03-14

完整的代码实现,尝试一下。

public class T {

    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static <T> List<T> parseObjectList(String json, Class<T> clazz) {
        try {
            return mapper.readValue(
                    json,
                    TypeFactory.defaultInstance().constructParametricType(ArrayList.class, clazz)
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> String toJsonString(T t) {
        try {
            return mapper.writeValueAsString(t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            String responseString = "[{\"ID\":\"1234\",\"CustomerName\":\"KUMAR\",\"BranchName\":\"HARBOUR\",\"SchemeName\":\"GOLD\",\"MobileNumber\":\"123456789\",\"CustomerType\":\"PRIMARY\",\"DedupeFound\":\"NO\"},{\"ID\":\"1234\",\"CustomerName\":\"SEAN\",\"BranchName\":\"HARBOUR\",\"SchemeName\":\"GOLD\",\"MobileNumber\":\"123456789\",\"CustomerType\":\"SECONDARY\",\"DedupeFound\":\"YES\"},{\"ID\":\"5678\",\"CustomerName\":\"MARK\",\"BranchName\":\"CANTONMENT\",\"SchemeName\":\"DIAMOND\",\"MobileNumber\":\"123456789\",\"CustomerType\":\"PRIMARY\",\"DedupeFound\":\"NO\"},{\"ID\":\"5678\",\"CustomerName\":\"STEVE\",\"BranchName\":\"CANTONMENT\",\"SchemeName\":\"DIAMOND\",\"MobileNumber\":\"123456789\",\"CustomerType\":\"SECONDARY\",\"DedupeFound\":\"YES\"}]";
            List<DedupeRes> list = parseObjectList(responseString, DedupeRes.class);
            if (list != null && !list.isEmpty()) {
                Map<String, List<DedupeRes>> map = list.stream().collect(Collectors.groupingBy(DedupeRes::getID));
                List<Res> resList = new ArrayList<>();
                map.forEach((k, v) -> resList.add(listToRes(v)));
                responseString = toJsonString(resList);
                System.out.println(responseString);
            }
        } catch (Exception e) {
            // xx
            e.printStackTrace();
        }
    }

    private static Res listToRes(List<DedupeRes> list) {
        Res res = new Res();
        res.setDedupeDetails(new ArrayList<>());
        for (DedupeRes dedupeRes : list) {
            if (Objects.equals("PRIMARY", dedupeRes.getCustomerType())) {
                // if you used spring -> BeanUtils.copyProperties(dedupeRes, res);
                res.setID(dedupeRes.getID());
                res.setBranchName(dedupeRes.getBranchName());
                res.setCustomerName(dedupeRes.getCustomerName());
                res.setMobileNumber(dedupeRes.getMobileNumber());
                res.setSchemeName(dedupeRes.getSchemeName());
            }
            DedupeDetails details = new DedupeDetails();
            //
            BeanUtils.copyProperties(dedupeRes, details);
            res.getDedupeDetails().add(details);
        }
        return res;
    }

    @Data
    private static class DedupeRes {
        @JsonProperty("ID")
        private String ID;
        @JsonProperty("CustomerName")
        private String CustomerName;
        @JsonProperty("BranchName")
        private String BranchName;
        @JsonProperty("SchemeName")
        private String SchemeName;
        @JsonProperty("MobileNumber")
        private String MobileNumber;
        @JsonProperty("CustomerType")
        private String CustomerType;
        @JsonProperty("DedupeFound")
        private String DedupeFound;
    }

    @Data
    private static class Res {
        @JsonProperty("ID")
        private String ID;
        @JsonProperty("CustomerName")
        private String CustomerName;
        @JsonProperty("BranchName")
        private String BranchName;
        @JsonProperty("SchemeName")
        private String SchemeName;
        @JsonProperty("MobileNumber")
        private String MobileNumber;
        @JsonProperty("dedupeDetails")
        private List<DedupeDetails> dedupeDetails;
    }

    @Data
    private static class DedupeDetails {
        @JsonProperty("CustomerType")
        private String CustomerType;
        @JsonProperty("CustomerName")
        private String CustomerName;
        @JsonProperty("DedupeFound")
        private String DedupeFound;
    }
}

康苏尔。

[{"ID":"1234","CustomerName":"KUMAR","BranchName":"HARBOUR","SchemeName":"GOLD","MobileNumber":"123456789","dedupeDetails":[{"CustomerType":"PRIMARY","CustomerName":"KUMAR","DedupeFound":"NO"},{"CustomerType":"SECONDARY","CustomerName":"SEAN","DedupeFound":"YES"}]},{"ID":"5678","CustomerName":"MARK","BranchName":"CANTONMENT","SchemeName":"DIAMOND","MobileNumber":"123456789","dedupeDetails":[{"CustomerType":"PRIMARY","CustomerName":"MARK","DedupeFound":"NO"},{"CustomerType":"SECONDARY","CustomerName":"STEVE","DedupeFound":"YES"}]}]
 类似资料:
  • 问题内容: 我有这样的JSON字符串 此JSON数组中有重复的值..我怎样才能使该JSON数组像这样唯一 我在寻找使用较少迭代的建议, 在这种情况下不起作用。 欢迎使用任何第三方库的建议。 问题答案: 在以下SO问题中检查解决方案: 使用jQuery从JSON数组获取唯一结果 您必须遍历数组并创建一个包含唯一值的新数组。

  • 我有两个RDDs。在Spark scala中,如果event1001RDD和event2009RDD具有相同的id,我该如何连接它们? Val事件1001RDD:模式RDD=[事件类型,id,位置,日期1] val event 2009 rdd:schemaRDD =[事件类型,id,日期1,日期2] 预期结果将是:(唯一)(按 id 排序) [事件类型,ID,1001 的位置,1001 的日期1

  • 因此,我有一个名为的对象数组,我需要获得每个球员的得分和犯规总数。 我的问题是我认为我使用了太多的 有没有更干净的解决方案? null null

  • 我的数组包含从0到整数的随机唯一数。最大值。 如何生成唯一的id/签名(int)来唯一地标识每个数组,而不是搜索每个数组并检查每个数字。 例如 每个数组可以有不同的长度,但数字在数组中不重复,可以在其他数组中重复。每个数组的唯一id的目的是通过id来识别它,以便快速进行搜索。数组包含组件的id,数组的唯一签名/id将标识其中包含的组件。 此外,无论数组中的值的顺序如何,生成的id应该是相同的。像{

  • 我创建了一个对象数组,如下所示: 我正在尝试创建一个新数组,该数组过滤位置,使其仅包含不具有相同城市属性的对象(lat/lng重复项可以)。是否有内置的JS或Jquery函数来实现这一点?

  • 问题内容: 我想知道将UUID转换为唯一整数的最简单方法是什么?我曾尝试使用哈希码,但有人告诉我,如果我使用哈希码,它将不会总是唯一的吗? 那么最简单的方法是什么?哈希码是否唯一? 问题答案: 由于UUID是128位,而int只有32位,因此您将遇到问题。您要么不得不承受碰撞的风险,然后尝试将其捏合到较小的空间(这可能是这样做的好方法),要么找到替代方法(直接使用,映射到-不知道为什么很难分辨)