按json数组分组,使用相同的键名C# [英] Group by json array result with same key name C#

查看:412
本文介绍了按json数组分组,使用相同的键名C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class NameDTO
{
    public string Name;
}

public class ValDTO
{
    public string Val;
}

_nameDetials = new List<NameDTO>();





进入List _nameDetials我得到密钥并进入_valDetails我得到了我在下面用于块的值并将它们添加到阵列。在下面的给定值中,我将每个列表计为20。



JArray jChildArray = new JArray();



Into List _nameDetials I get keys and into _valDetails I get values for which I used below for block and added them to array. In my given values below i get count as 20 into each of the lists.

JArray jChildArray = new JArray();

for (int i = 0; i < Math.Max(_nameDetials.Count, _valDetials.Count); i++)
{
    JObject jChildObject = new JObject();
    jChildObject.Add(_nameDetials[i].Name, _valDetials[i].Val);
    jChildArray.Add(jChildObject);
}





我有一个json数组,如下所示



I have a json array as shown below

[
  {
    "message-Code": "   0"
  },
  {
    "msg-Number-Pos1": "0"
  },
  {
    "msg-Number-Pos2": "0"
  },
  {
    "msg-Number-Pos3": " "
  },
  {
    "message-Code": "   0"
  },
  {
    "msg-Number-Pos1": "0"
  },
  {
    "msg-Number-Pos2": "0"
  },
  {
    "msg-Number-Pos3": " "
  },
  {
    "message-Code": "   0"
  },
  {
    "msg-Number-Pos1": "0"
  },
  {
    "msg-Number-Pos2": "0"
  },
  {
    "msg-Number-Pos3": " "
  },
  {
    "message-Code": "   0"
  },
  {
    "msg-Number-Pos1": "0"
  },
  {
    "msg-Number-Pos2": "0"
  },
  {
    "msg-Number-Pos3": " "
  },
  {
    "message-Code": "   0"
  },
  {
    "msg-Number-Pos1": "0"
  },
  {
    "msg-Number-Pos2": "0"
  },
  {
    "msg-Number-Pos3": "0"
  }
]



我想将这些数据分组以得到如下所示的结果。 />



I want to group this data to get the result as shown below.

[
    {
      "message-Code": "  0",
      "msg-Number-Pos1": "0",
      "msg-Number-Pos2": "0",
      "msg-Number-Pos3": "0"
    },
    {
      "message-Code": "  0",
      "msg-Number-Pos1": "0",
      "msg-Number-Pos2": "0",
      "msg-Number-Pos3": "0"
    },
    {
      "message-Code": "  0",
      "msg-Number-Pos1": "0",
      "msg-Number-Pos2": "0",
      "msg-Number-Pos3": "0"
    },
    {
      "message-Code": "  0",
      "msg-Number-Pos1": "0",
      "msg-Number-Pos2": "0",
      "msg-Number-Pos3": "0"
    },
    {
      "message-Code": "  0",
      "msg-Number-Pos1": "0",
      "msg-Number-Pos2": "0",
      "msg-Number-Pos3": "0"
    }
]





我想基于相同的名字进行分组。





我无法追踪到我所缺少的东西。或者我可以在我的情况下,我们将一个json数组分成相等的数字,即4。

我是c#的新手,无法找到匹配的代码。

任何人都可以帮忙吗?谢谢。



我尝试了什么:



我试过以下代码





I want to group based on same key names.


I am unable to trace what is that I am missing.Or can we group a json array into equal number i.e. 4 in my case.
I am new to c# and unable to find a matching code.
Can anyone help? Thanks.

What I have tried:

I tried the below code

var grouparray = (from t in jChildArray
                  group t by new { t }
                  into grp
                  select grp.Key.t).ToList();

推荐答案

分组具有类似的特征。你想要相反的。没有任何功能可以为你做这件事。



这就是原因:



你有一个阵列。我们知道的第一个message-Code应该使用第一个msg-Number-Pos1,但是就阵列而言,这些只是字符串。



有些方法可以欺骗。如果您知道信息是以4组为单位,那么您可以使用各种索引对它们进行分组:



Grouping takes similar characteristics. You want the opposite. There is no function that will ever do this for you.

Here's why:

You have an array . The first "message-Code" we know should go with the first "msg-Number-Pos1", but as far as the array is concerned, these are just strings.

There are ways you can "cheat" tho. If you KNOW that the info comes in sets of 4's then you can use an index, of sorts, to group them:

var grouparray =
    jChildArray
        .Select((pair,i)=>new {index = i/4, pair})
        .GroupBy(a=>a.index);



这会让你得到每组4的分组。

请注意,我认为这是对一个简单问题的错误解决方法



一开始,你现在只有一个字符串对数组的数组。为什么不首先解决JArray的问题?


This gets you a grouping with each set of 4.
Be aware that I would consider this a bad fix to a simple problem

For a start, you now only have an array of arrays of string pairs. Why not fix the problem with the JArray in the first place?


这样的事情:

How about something like this:
JObject jChildObject = null;

// NB: Can't use "Max" here, or you risk going beyond the end of one of the arrays:
int length = Math.Min(_nameDetails.Count, _valDetails.Count); 

for (int i = 0; i < length; i++)
{
    string name = _nameDetails[i].Name;
    if (jChildObject == null)
    {
        jChildObject = new JObject();
    }
    else if (string.Equals(name, "message-Code", StringComparison.Ordinal))
    {
        jChildArray.Add(jChildObject);
        jChildObject = new JObject();
    }
    
    jChildObject.Add(name, _valDetials[i].Val);
}

if (jChildObject != null)
{
    jChildArray.Add(jChildObject);
}


这篇关于按json数组分组,使用相同的键名C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆