[英]Creating single Array of objects from multiple array objects in c#
I have a web API response like below and I need to move all objects into a single array in C# ASP.NET我有一个像下面这样的 Web API 响应,我需要将所有对象移动到 C# ASP.NET 中的单个数组中
[
[
{
"id":23,
"name":"John",
"email":"John@domain.com",
"appointment_date":"tomorrow",
"appointment_category":3,
"time":"morning"
},
{
"id":35,
"name":"John",
"email":"John@domain.com",
"appointment_date":"tomorrow",
"appointment_category":4,
"time":"afternoon"
}
],
[
{
"id":17,
"name":"Alex",
"email":"Alex @domain.com",
"appointment_date":"tomorrow",
"appointment_category":3,
"time":"morning"
}
],
[
{
"id":22,
"name":"Bob",
"email":"Bob@domain.com",
"appointment_date":"tomorrow",
"appointment_category":5,
"time":"morning"
}
]
]
I want to move all objects into single array.我想将所有对象移动到单个数组中。 Like below
像下面
[
{
"id":23,
"name":"John",
"email":"John@domain.com",
"appointment_date":"tomorrow",
"appointment_category":3,
"time":"morning"
},
{
"id":17,
"name":"John",
"email":"John@domain.com",
"appointment_date":"tomorrow",
"appointment_category":3,
"time":"morning"
},
{
"id":17,
"name":"John",
"email":"John@domain.com",
"appointment_date":"tomorrow",
"appointment_category":3,
"time":"morning"
}
]
Please help me Thank u请帮助我谢谢你
Not sure if you have deserialized the json already.不确定您是否已经反序列化了 json。 But you can do it like this.
但是你可以这样做。 Create 2 classes and deserialize with Newtonsoft.Json.
创建 2 个类并使用 Newtonsoft.Json 进行反序列化。 Then use
Linq
with SelectMany
to get a list of single object.然后使用带有
SelectMany
Linq
来获取单个对象的列表。
//deseralize the json
var list1 = JsonConvert.DeserializeObject<List<List<Class2>>>(json);
//select all the nested items
var list2 = list1.SelectMany(x => x).ToList();
The classes班级
public class Class1
{
public List<Class1> list { get; set; }
}
public class Class2
{
public int id { get; set; }
public string name { get; set; }
public string email { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.