[英]Creating single Array of objects from multiple array objects in c#
我有一個像下面這樣的 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"
}
]
]
我想將所有對象移動到單個數組中。 像下面
[
{
"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"
}
]
請幫助我謝謝你
不確定您是否已經反序列化了 json。 但是你可以這樣做。 創建 2 個類並使用 Newtonsoft.Json 進行反序列化。 然后使用帶有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();
班級
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.