Click here to Skip to main content
15,914,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public class NameDTO
{
    public string Name;
}

public class ValDTO
{
    public string Val;
}

_nameDetials = new List<NameDTO>();


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);
}


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"
    }
]


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();
Posted
Updated 10-Jul-17 7:24am
v2

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:

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

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?
 
Share this answer
 
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);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900