如何在 C# 中按 ID 对 JSON 数据进行分组

Posted

技术标签:

【中文标题】如何在 C# 中按 ID 对 JSON 数据进行分组【英文标题】:How to group JSON data by ID in C# 【发布时间】:2021-11-17 08:40:51 【问题描述】:

将数据表中的数据序列化后获取这种格式的JSON

这是读取excel文件并将数据存储在数据表中后的结果。后来使用 newtosoft.json 序列化成下面的 JSON 输出

JSON 现在低于:

[ "Id": "1", "Profit": "33.332999999999998", "Telephone": "123", "Email": "user1@testmail.com" ,  "Id": "1", "Profit": "21.21", "Telephone": "43", "Email": "user11@testmail.com" ,  "Id": "2", "Profit": "49.000999999999998", "Telephone": "22", "Email": "user2@testmail.com" ,  "Id": "2", "Profit": "10.1", "Telephone": "876", "Email": "user22@testmail.com" ]

预期格式

[ "Id": "1", "Profits": ["33.332999999999998", "21.21"], "Telephones": ["43", "123"], "Emails": ["user1@testmail.com", "user11@testmail.com"] ,  "Id": "2", "Profits": ["49.000999999999998", "10.1"], "Telephones": ["876", "22"], "Emails": ["user2@testmail.com", "user22@testmail.com"] ]

有人可以帮忙吗?

【问题讨论】:

这个问题更多地与您如何读取/存储/序列化数据有关,而不是与数据本身有关。您应该提供 部分代码,因为它是问题所在。 【参考方案1】:

试试这个

var json=...origin json
var jD = JsonConvert.DeserializeObject<DataOrigin[]>(json);
jD=jD.OrderBy(d => d.Id).ToArray();
    var prevId=string.Empty;
    var list=new List<Data>();
    foreach (var item in jD)
    
                
        if(item.Id!=prevId)
        
            prevId=item.Id;
            list.Add(new Data(Convert.ToInt32(item.Id), item.Profit, item.Telephone, item.Email));
        
        else
        
            var prevIdInt=Convert.ToInt32(prevId);
            var prevItem=list.Last(l =>l.Id==prevIdInt );
            prevItem.MergeItem(item.Profit,item.Telephone,item.Email);
        
    

    var result =  JsonConvert.SerializeObject(list);
    

结果

[
  
    "Id": 1,
    "Profits": [
      33.333,
      21.21
    ],
    "Telephones": [
      "123",
      "43"
    ],
    "Emails": [
      "user1@testmail.com",
      "user11@testmail.com"
    ]
  ,
  
    "Id": 2,
    "Profits": [
      49.001,
      10.1
    ],
    "Telephones": [
      "22",
      "876"
    ],
    "Emails": [
      "user2@testmail.com",
      "user22@testmail.com"
    ]
  
]

public class DataOrigin
    
        public string Id  get; set; 
        public double Profit  get; set; 
        public string Telephone  get; set; 
        public string Email  get; set; 

public class Data

    public int Id  get; set; 
    public  List<double> Profits  get; set; 
    public List<string> Telephones  get; set; 
    public List<string> Emails  get; set; 
    
    public Data(int id,double profit,string phone, string email)
      
         Id=id;
        Profits = new List<double>()profit;
        Telephones = new List<string>()phone;
        Emails = new List<string>()email;
        
    
    public void MergeItem (double profit,string phone, string email)
    
        Profits.Add(profit);
        Telephones.Add(phone);
        Emails.Add(email);
    

【讨论】:

@nitish 欢迎您。请不要忘记接受答案。您可以通过单击我的答案旁边的复选标记来完成。【参考方案2】:

解决办法如下:

变量数据 = [ “身份证”:“1”, “利润”:“33.332999999999998”, “电话”:“123”, “电子邮件”:“user1@testmail.com” , “身份证”:“1”, “利润”:“21.21”, “电话”:“43”, “电子邮件”:“user11@testmail.com” , “身份证”:“2”, “利润”:“49.000999999999998”, “电话”:“22”, “电子邮件”:“user2@testmail.com” , “身份证”:“2”, “利润”:“10.1”, “电话”:“876”, “电子邮件”:“user22@testmail.com” ] 变种温度 = ; data.forEach(x => if (temp[x.Id] == undefined) 临时[x.Id] = temp[x.Id]['Id'] = x.Id temp[x.Id]['利润'] = [] temp[x.Id]['电话'] = [] temp[x.Id]['Email'] = [] temp[x.Id].Profit.push(x.Profit) temp[x.Id].Telephone.push(x.Telephone) temp[x.Id].Email.push(x.Email) ) var finalResponse = [] for (const [key, value] of Object.entries(temp)) finalResponse.push(值)

【讨论】:

以上是关于如何在 C# 中按 ID 对 JSON 数据进行分组的主要内容,如果未能解决你的问题,请参考以下文章

如何在javascripts中按经纬度距离对数组项进行排序?

如何在 swift 中按字母顺序对 JSON 字符串进行排序?

如何在 C# 中按升序对多维数组进行排序? [复制]

如何使用 jQuery 对 JSON 数据进行分页?

如何在c#中按两列对DataTable进行排序

如何在 Reactjs 中按升序或降序对数据进行排序?