对对象列表进行排序并转换为 JObjects 列表
Sort a list of objects and convert into a list of JObjects
我是 C# 编程新手,如有任何帮助,我将不胜感激。因为这对 C# 开发人员来说可能是一件简单的事情。我已经尝试了很多东西,但是当我尝试开发它时在 Newtonsoft JObjects 中出现类型不匹配或编译错误。
我有一个对象列表,例如
List<ContextResult> formOutputs ->
[0] = (section 1, Button, true),
[1] = (section 1, TextBox, anyname),
[2] = (section 2, Button, false)
public class ContextResult
{
public string Section { get; set; }
public string Key{ get; set; }
public string Value { get; set; }
}
我需要将其分类为多个部分,然后使用 Newtonsoft.Json.Linq 将其转换为 JObject 列表。 JSON 格式的输出应如下所示,
"section 1":{
"Button": "true",
"TextBox": "anyname"
},
"section 2":{
"Button": "false"
}
请注意,我已经将 formOutputs 按升序排序并使用 GroupBy
分组以删除重复项。
我不会使用 JObjects,而是将您的数据转换成所需的结构,然后将其序列化。
以下是如何处理您的数据:
var o = formOutputs
.GroupBy(o => o.Section)
.ToDictionary(g => g.Key,g => g.ToDictionary(x => x.Key, x => x.Value));
这是一个带有评论的版本:
var o = formOutputs
.GroupBy( o => o.Section) // For the example data we now have groups: 'section 1', 'section 2'
.ToDictionary(
keySelector: g => g.Key, // this 'Key' is the group's key (so o.Section), not the Key property of ContextResult
elementSelector: g => g.ToDictionary(
keySelector: x => x.Key,
elementSelector: x => x.Value));
测试:
var formOutputs = new List<ContextResult> {
new ContextResult { Section = "section 1", Key = "Button", Value = "true"},
new ContextResult { Section = "section 1", Key = "TextBox",Value = "anyname"},
new ContextResult { Section = "section 2", Key = "Button", Value = "false"}
};
var o = formOutputs
.GroupBy(o => o.Section)
.ToDictionary(g => g.Key,g => g.ToDictionary(x => x.Key, x => x.Value));
Console.WriteLine(JsonConvert.SerializeObject(o, Formatting.Indented));
输出:
{
"section 1": {
"Button": "true",
"TextBox": "anyname"
},
"section 2": {
"Button": "false"
}
}
我是 C# 编程新手,如有任何帮助,我将不胜感激。因为这对 C# 开发人员来说可能是一件简单的事情。我已经尝试了很多东西,但是当我尝试开发它时在 Newtonsoft JObjects 中出现类型不匹配或编译错误。
我有一个对象列表,例如
List<ContextResult> formOutputs ->
[0] = (section 1, Button, true),
[1] = (section 1, TextBox, anyname),
[2] = (section 2, Button, false)
public class ContextResult
{
public string Section { get; set; }
public string Key{ get; set; }
public string Value { get; set; }
}
我需要将其分类为多个部分,然后使用 Newtonsoft.Json.Linq 将其转换为 JObject 列表。 JSON 格式的输出应如下所示,
"section 1":{
"Button": "true",
"TextBox": "anyname"
},
"section 2":{
"Button": "false"
}
请注意,我已经将 formOutputs 按升序排序并使用 GroupBy
分组以删除重复项。
我不会使用 JObjects,而是将您的数据转换成所需的结构,然后将其序列化。
以下是如何处理您的数据:
var o = formOutputs
.GroupBy(o => o.Section)
.ToDictionary(g => g.Key,g => g.ToDictionary(x => x.Key, x => x.Value));
这是一个带有评论的版本:
var o = formOutputs
.GroupBy( o => o.Section) // For the example data we now have groups: 'section 1', 'section 2'
.ToDictionary(
keySelector: g => g.Key, // this 'Key' is the group's key (so o.Section), not the Key property of ContextResult
elementSelector: g => g.ToDictionary(
keySelector: x => x.Key,
elementSelector: x => x.Value));
测试:
var formOutputs = new List<ContextResult> {
new ContextResult { Section = "section 1", Key = "Button", Value = "true"},
new ContextResult { Section = "section 1", Key = "TextBox",Value = "anyname"},
new ContextResult { Section = "section 2", Key = "Button", Value = "false"}
};
var o = formOutputs
.GroupBy(o => o.Section)
.ToDictionary(g => g.Key,g => g.ToDictionary(x => x.Key, x => x.Value));
Console.WriteLine(JsonConvert.SerializeObject(o, Formatting.Indented));
输出:
{
"section 1": {
"Button": "true",
"TextBox": "anyname"
},
"section 2": {
"Button": "false"
}
}