如何< string>在C#中将List转换为JSON格式

nimxete2  于 2023-05-19  发布在  C#
关注(0)|答案(3)|浏览(289)

我有一个字符串列表,其中包含以下项目:

  • 001-HA-经理
  • 001-HA-主管
  • 001-HA-确认
  • 001-HA-文件审查员
  • 001-HA-经理评审员
  • 002-HA-经理
  • 002-HA-主管
  • 002-HA-确认

我需要那些项目在下面的JSON格式:

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

}de这里

zbwhf8kr

zbwhf8kr1#

你可以试试这个代码

string json = JsonConvert.SerializeObject( new
    {
    Project = list.Select(l => l.Split("-"))
                  .Select(l => new { Key = l[0], Value = l[2] })
                  .GroupBy(g => g.Key, g => g.Value)
                  .Select(g => new { ProjectCode = g.Key, Groups = g })
    }, Newtonsoft.Json.Formatting.Indented);
crcmnpdw

crcmnpdw2#

使用传统的方法,不使用任何一行程序。我已经创建了相应的Model类来保存您的数据,并根据需要将其标记化,以将相应的字段填写到Model类中:
小提琴: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;}
}

输出:{"Project":[{"ProjectCode":"001","Groups":["Manager","Supervisor","Validation","DocumentReviewer","ManagerReviewer"]},{"ProjectCode":"002","Groups":["Manager","Supervisor","Validation"]}]}
Jsonlint验证后:

nfs0ujit

nfs0ujit3#

首先创建类
然后,定义一个构造函数来接收数据,然后分离项目和组,然后对项目进行分组并创建Jason
我使用Newtonsoft.json和Linq
1.创建类

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.添加数据

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 使用linq创建json

var listall = _li
          .GroupBy(d => d.ProjectCode)
           .Select(g => new {
            ProjectCode = g.Key,
          Groups = g.Select(d => d.Group)
     }).ToList();
     
var jsonString = "{\r\n\"Project\":" + JsonConvert.SerializeObject(listall , Formatting.Indented) + "}";

相关问题