gpt4 book ai didi

c# - 使用 Newtonsoft.Json 将两个数组转换为一个 JSON 对象

转载 作者:太空狗 更新时间:2023-10-29 20:20:11 27 4
gpt4 key购买 nike

我有数组 name[]lastname[] .如何将它们合并并转换为 JSON 字符串?我希望它采用以下格式。我需要 JSON 中的“员工”头衔。

{ "Employees" : [
{"name": "John", "lastname": "Coleman"},
{"name": "Chip", "lastname": "Dale"},
{"name": "Ann", "lastname": "Smith"},
{"name": "Terry", "lastname": "Johnson"},
{"name": "Mary", "lastname": "Loggins"},
{"name": "Timothy", "lastname": "Lopez"},
{"name": "Jessica", "lastname": "Brown"}
]}

我需要一种有效的方法来执行此操作,因为数组中有很多项。我实际上有两个以上的数组需要组合成一个 JSON 对象。为简单起见,我用两个演示了我想要的东西。它们都具有相同数量的项目并已订购。我不想自己迭代数组和构造 JSON 字符串。

更新:

我忘了说我的数组是 IEnumerable<[]>字符串和整数数组。这是我尝试在另一个类中创建的数组。

  public string[] Name {
get{ return (Employees ?? Enumerable.Empty<Employee> ()).Select (p => p.name).ToArray(); }
}

public string[] Lastname {
get{ return (Employees ?? Enumerable.Empty<Employee> ()).Select (p => p.lastname).ToArray(); }
}

public int[] Age {
get{ return (Employees ?? Enumerable.Empty<Employee> ()).Select (p => p.age).ToArray(); }
}

然后我访问它们

var name = X.Select(s => s.Name).ToArray();
var lastname = X.Select(s => s.Lastname).ToArray();
var age = X.Select(s => s.Age).ToArray();

var employees = new { Employees = Enumerable.Range(0, name.Length).Select(i => new { name = name[i], lastname = lastname[i], age = age[i] }) };
var json = JsonConvert.SerializeObject(employees, Formatting.Indented);
Debug.WriteLine(json);

出于某种原因,这会返回类似于

的内容
{"Employees":[{"name":["John","Chip","Ann","Terry"],"lastname":["Coleman","Dale","Smith","Johnson"],"age":[42, 26, 33, 24]}]}

所有名字,姓氏都放在一起的地方。我如何获得正确的格式?

最佳答案

您可以将它们与 Zip() 结合使用进入anonymous type ,然后序列化:

        string[] name = new string[] { "John", "Chip" };
string[] lastname = new string[] { "Coleman", "Dale" };

var employees = new { Employees = name.Zip(lastname, (n1, n2) => new { name = n1, lastname = n2 }) };
var json = JsonConvert.SerializeObject(employees, Formatting.Indented);
Debug.WriteLine(json);

哪些输出:

{
"Employees": [
{
"name": "John",
"lastname": "Coleman"
},
{
"name": "Chip",
"lastname": "Dale"
}
]
}

对于多个数组,使用 Enumerable.Range() 可能更容易并行遍历数组:

        string[] name = new string[] { "John", "Chip" };
string[] lastname = new string[] { "Coleman", "Dale" };
string[] title = new string[] { "Mr", "Dr" };
string[] profession = new string[] { "Coder", "Doctor" };

var employees2 = new { Employees = Enumerable.Range(0, name.Length).Select(i => new { title = title[i], name = name[i], lastname = lastname[i], profession = profession[i] }) };
var json2 = JsonConvert.SerializeObject(employees2, Formatting.Indented);
Debug.WriteLine(json2);

更新

如果您的字符串在 IEnumerable<String[]> 中,您可以将该外部可枚举转换为数组,然后对其进行索引。例如,给定以下测试用例:

        string[] name = new string[] { "John", "Chip" };
string[] lastname = new string[] { "Coleman", "Dale" };
string[] title = new string[] { "Mr", "Dr" };
string[] profession = new string[] { "Coder", "Doctor" };

IEnumerable<string[]> strings = new[] { title, name, lastname, profession };

你可以这样做:

        var stringArray = strings.ToArray();

var employees2 = new { Employees = Enumerable.Range(0, name.Length).Select(i => new { title = stringArray[0][i], name = stringArray[1][i], lastname = stringArray[2][i], profession = stringArray[3][i] }) };
var json2 = JsonConvert.SerializeObject(employees2, Formatting.Indented);
Debug.WriteLine(json2);

结果是:

{
"Employees": [
{
"title": "Mr",
"name": "John",
"lastname": "Coleman",
"profession": "Coder"
},
{
"title": "Dr",
"name": "Chip",
"lastname": "Dale",
"profession": "Doctor"
}
]
}

更新 2

如果您实际上有一个包含员工枚举的对象枚举,您可以使用 Enumerable.SelectMany 将它们展平.例如,给定以下类:

public class Employee
{
public string name { get; set; }
public string lastname { get; set; }
public int age { get; set; }
public string someMoreDataThatShouldNotBeSerialized { get; set; }
}

public class EmployeeContainer
{
public IEnumerable<Employee> Employees { get; set; }
}

您可以按如下方式将它们展平:

        var X = GetAllEmployees();

var employees = X.SelectMany(s => s.Employees ?? Enumerable.Empty<Employee>()).Select(e => new { name = e.name, lastname = e.lastname, age = e.age });
var json = JsonConvert.SerializeObject(employees, Formatting.Indented);
Debug.WriteLine(json);

然后是测试设置

    public static IEnumerable<EmployeeContainer> GetAllEmployees()
{
return new[] {
new EmployeeContainer {
Employees =
new[] {
new Employee { name = "John", lastname = "Coleman", age = 42, someMoreDataThatShouldNotBeSerialized = "someMoreData1" },
new Employee { name = "Chip", lastname = "Dale", age = 26, someMoreDataThatShouldNotBeSerialized = "someMoreData2" },
}
},
new EmployeeContainer {
Employees =
new[] {
new Employee { name = "Ann", lastname = "Smith", age = 33, someMoreDataThatShouldNotBeSerialized = "someMoreData3" },
new Employee { name = "Terry", lastname = "Johnson", age = 24, someMoreDataThatShouldNotBeSerialized = "someMoreData4" },
}
},
new EmployeeContainer()
};
}

产生:

[
{
"name": "John",
"lastname": "Coleman",
"age": 42
},
{
"name": "Chip",
"lastname": "Dale",
"age": 26
},
{
"name": "Ann",
"lastname": "Smith",
"age": 33
},
{
"name": "Terry",
"lastname": "Johnson",
"age": 24
}
]

工作 fiddle .

关于c# - 使用 Newtonsoft.Json 将两个数组转换为一个 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com