skip to Main Content

I have a List of string that contains the below items:

  • 001-HA-Manager
  • 001-HA-Supervisor
  • 001-HA-Validation
  • 001-HA-DocumentReviewer
  • 001-HA-ManagerReviewer
  • 002-HA-Manager
  • 002-HA-Supervisor
  • 002-HA-Validation

I need to those item on the below JSON format:

{
"Project": [
  {
    "ProjectCode": "001",
    "Groups": ["Manager", "Supervisor", "Validation", "DocumentReviewer","ManagerReviewer"]
  },
  {
     "ProjectCode": "002",
     "Groups": ["Manager", "Supervisor", "Validation"]
    }
]

}de here

3

Answers


  1. you can try this code

        var json = JsonConvert.SerializeObject( new
        {
            Project = (list.Select(l => new { Key = l.Substring(0, 3), Value = l.Substring(4) })
                             .GroupBy(g => g.Key, g => g.Value)
                             .Select(g => new { ProjectCode = g.Key, Groups = g.ToList() })
            )}, Newtonsoft.Json.Formatting.Indented);
    
    Login or Signup to reply.
  2. Using a traditional approach without any one liners. I have created corresponding Model classes to hold your data and the tokenize it as required to fill out the respective fields into the Model class:

    Fiddle: https://dotnetfiddle.net/XH28FX

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Newtonsoft.Json;
                        
    public class Program
    {
        public static void Main()
        {
            List<string> input=new List<string>();
            input.Add("001-HA-Manager");
            input.Add("001-HA-Supervisor");
            input.Add("001-HA-Validation");
            input.Add("001-HA-DocumentReviewer");
            input.Add("001-HA-ManagerReviewer");
            input.Add("002-HA-Manager");
            input.Add("002-HA-Supervisor");
            input.Add("002-HA-Validation");
            
            List<Lookup> lookup= new List<Lookup>();
            foreach(var item in input)
            {
                Lookup lookupitem=new Lookup();
                var tokenizeditem=item.Split('-');
                lookupitem.ProjectCode=tokenizeditem[0];
                lookupitem.Title=tokenizeditem[2];
                lookup.Add(lookupitem); 
            }
            
            //Now group by:
            var test= from p in lookup
                  group p.Title by p.ProjectCode into g
                  select new Project { ProjectCode = g.Key, Groups = g.ToList() };
            var customDataObj = new { Project = test };
            var jstring= JsonConvert.SerializeObject(customDataObj);
            Console.WriteLine(jstring); 
        }
    }
    
    public class Project
    {
        public string ProjectCode {get;set;}
        public List<string> Groups {get;set;}
    }
    
    public class Lookup
    {
        public string ProjectCode {get;set;}
        public string Title {get;set;}
    }
    

    Output: {"Project":[{"ProjectCode":"001","Groups":["Manager","Supervisor","Validation","DocumentReviewer","ManagerReviewer"]},{"ProjectCode":"002","Groups":["Manager","Supervisor","Validation"]}]}

    After validating from Jsonlint:
    enter image description here

    Login or Signup to reply.
  3. First create class

    then ,define a constructor to receive the data and then separate the project and the group And then group the project and create Jason

    I use Newtonsoft.Json and Linq

    1.create class

     public class MyDate
            {
                public MyDate(string All)
                {
                    this.All = All;
                }
                public string ProjectCode
                {
                    get { return this.All.Split('-')[0]; }
                }
    
                public string Group
                {
                    get { return this.All.Split('-')[2]; }
                }
               
                public string All { set; get; }
            }
    

    2.Add data

      List< MyDate> _li = new List<MyDate> 
    { new MyDate("001-HA-Manager"), new MyDate("001-HA-Validation"),
               new MyDate("001-HA-Supervisor"),
    new MyDate("001-HA-Validation"),
    new MyDate("001-HA-DocumentReviewer")
               ,new MyDate("001-HA-ManagerReviewer") 
     ,new MyDate("002-HA-Manager") 
     ,new MyDate("002-HA-Supervisor")
                ,new MyDate("002-HA-Validation")
               };
    

    3.group by with linq and create json

     var listall = _li
              .GroupBy(d => d.ProjectCode)
               .Select(g => new {
                ProjectCode = g.Key,
              Groups = g.Select(d => d.Group)
         }).ToList();
         
    var jsonString = "{rn"Project":" + JsonConvert.SerializeObject(listall , Formatting.Indented) + "}";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search