Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi folks, can anyone help me to achieve the output like this. i cant able to set the array header like below. suggest me the best way and solution either sample source
My Code:
    var groupedCustomerList = _commonService.GetMultilingualDatas(language)
.GroupBy(u => u.screen_name)
.Select(grp => grp.ToList())
.ToList();
    string output = Newtonsoft.Json.JsonConvert.SerializeObject(groupedCustomerList);


Actual Result i'm getting:
[
  [
    {
      "multilingual_id": 1,
      "screen_name": "splash_screen",
      "key": "care_any_ware",
      "max_length": 0,
      "value": "Care anywhere",
      "comments": "",
      "language": "en-gb"
    }
  ],
  [
    {
      "multilingual_id": 2,
      "screen_name": "login_start",
      "key": "log_in",
      "max_length": 0,
      "value": "Log in",
      "comments": "",
      "language": "en-gb"
    },
    {
      "multilingual_id": 3,
      "screen_name": "login_start",
      "key": "join_today",
      "max_length": 0,
      "value": "Join today",
      "comments": "",
      "language": "en-gb"
    },
    {
      "multilingual_id": 4,
      "screen_name": "login_start",
      "key": "take_a_quick_tour",
      "max_length": 0,
      "value": "Take a quick tour",
      "comments": "",
      "language": "en-gb"
    }
  ],



Expected Result:
{
  "splash_screen": [
    {
      "key": "care_any_ware",
      "max_length": "25",
      "value": "Care anywhere",
      "value_en": "Care anywhere",
      "comments": ""
    }
  ],
  <big>"login_start"</big>: [
    {
      "key": "log_in",
      "max_length": "35",
      "value": "Log in",
      "value_en": "Log in",
      "comments": ""
    },
    {
      "key": "join_today",
      "max_length": "35",
      "value": "Join today",
      "value_en": "Join today",
      "comments": ""
    },
    {
      "key": "take_a_quick_tour",
      "max_length": "35",
      "value": "Take a quick tour",
      "value_en": "Take a quick tour",
      "comments": ""
    }
  ],


What I have tried:

*Tried with list as property in a class
*Raju's Blog - Creating N-Tier ASP.NET Web API application[^]
How to get a Name to a Json Array in web API[^]
few more stackoverflow links
Posted
Updated 30-Aug-18 3:03am
v2

1 solution

Convert the data to a dictionary instead of a list:
C#
var groupedCustomerList = _commonService.GetMultilingualDatas(language)
    .GroupBy(u => u.screen_name)
    .ToDictionary(g => g.Key);

string output = Newtonsoft.Json.JsonConvert.SerializeObject(groupedCustomerList);

NB: You'll still end up with the screen_name property on each item. If you want to remove that, you can project the group elements to an anonymous class:
C#
.GroupBy(u => u.screen_name, u => new
{
    u.key,
    u.max_length,
    u.value,
    u.value_en,
    u.comments,
})
(Or just add the [JsonIgnore] attribute to that property.)
 
Share this answer
 
v2

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