1
public class NameDTO { public string Name; } public class ValDTO { public string Val; } _nameDetials = new List<NameDTO>(); _valDetials = new List<ValDTO>(); 

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 tried the below code

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

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.

2
  • Use something like group t by t.messageCode and select grp Commented Jul 10, 2017 at 6:45
  • 2
    What is the algorithm that groups the first array into the second one? I can't find any pattern connecting both sets... Commented Jul 10, 2017 at 7:52

1 Answer 1

1

It seems that you do not want to group by the key but rather by number of items. Can you try:

 private string Serialize(List<NameDTO> _nameDetials, List<ValDTO> _valDetials) { JArray jChildArray = new JArray(); for (int i = 0; i < Math.Max(_nameDetials.Count, _valDetials.Count) / 4; i++) { var jChildObject = new JObject(); for (int j = 0; j < 4; j++) jChildObject.Add(_nameDetials[i * 4 + j].Name, _valDetials[i * 4 + j].Val); jChildArray.Add(jChildObject); } return JsonConvert.SerializeObject(jChildArray); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Pablo. You made my day. That 4 is coming from config file, so no need to bother about that since it is a dynamic value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.