英文:
How to convert List<string> to JSon format in C#
问题
我有一个包含以下项目的字符串列表:
- 001-HA-Manager
- 001-HA-Supervisor
- 001-HA-Validation
- 001-HA-DocumentReviewer
- 001-HA-ManagerReviewer
- 002-HA-Manager
- 002-HA-Supervisor
- 002-HA-Validation
我需要将这些项目转换成以下的JSON格式:
{
"Project": [
{
"ProjectCode": "001",
"Groups": ["Manager", "Supervisor", "Validation", "DocumentReviewer", "ManagerReviewer"]
},
{
"ProjectCode": "002",
"Groups": ["Manager", "Supervisor", "Validation"]
}
]
}
英文:
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
答案1
得分: 1
你可以尝试这段代码
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);
英文:
you can try this code
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);
</details>
# 答案2
**得分**: 1
以下是翻译好的内容:
使用传统方法而没有任何一行代码。我已经创建了相应的“Model”类来保存您的数据并根据需要对其进行标记,以填充到“Model”类的相应字段中:
Fiddle链接:https://dotnetfiddle.net/XH28FX
```csharp
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);
}
//现在按项目代码分组:
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](https://jsonlint.com/)上验证后:
[![进入图像描述][2]][2]
[2]: https://i.stack.imgur.com/H7hMQ.png
<details>
<summary>英文:</summary>
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][1]:
[![enter image description here][2]][2]
[1]: https://jsonlint.com/
[2]: https://i.stack.imgur.com/H7hMQ.png
</details>
# 答案3
**得分**: 0
以下是您要翻译的内容:
1. 创建类
然后,定义一个构造函数来接收数据,然后分离项目和组,然后对项目进行分组并创建 JSON。
我使用 Newtonsoft.Json 和 Linq。
2. 创建类
```csharp
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; }
}
```
3. 添加数据
```csharp
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")
};
```
4. 使用 Linq 进行分组并创建 JSON
```csharp
var listall = _li
.GroupBy(d => d.ProjectCode)
.Select(g => new {
ProjectCode = g.Key,
Groups = g.Select(d => d.Group)
}).ToList();
var jsonString = "{\"Project\":" + JsonConvert.SerializeObject(listall, Formatting.Indented) + "}";
```
请注意,我已经将代码部分保留为原文,只对注释进行了翻译。
<details>
<summary>英文:</summary>
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 = "{\r\n\"Project\":" + JsonConvert.SerializeObject(listall , Formatting.Indented) + "}";
```
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论