在 c# asp .net core 中将 json 反序列化为 IEnumerable

我有一个 json 对象。我想将它反序列化为该IEnumerable类型的对象。

我在asp.net Core 工作。

我是这样尝试的:

IEnumerable div = (IEnumerable)JsonConvert.DeserializeObject(value, typeof(IEnumerable));

这里的值是 json 变量。

我得到了这种类型的异常:

Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型'System.Collections.IEnumerable',因为该类型需要一个 JSON 数组(例如 [1,2,3] ) 以正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型)可以从 JSON 对象反序列化的数组或列表)。JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。


人到中年有点甜
浏览 456回答 3
3回答

慕哥6287543

1) 如果您的 json 是一个对象,例如 MyUserpublic class MyUser{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public string Description { get; set; }}你的json是{&nbsp; &nbsp;"Id": 1,&nbsp; &nbsp;"Name": "Andrew",&nbsp; &nbsp;"Description": "Software Engineer"}然后您可以使用以下代码轻松反序列化MyUser user = JsonConvert.DeserializeObject<MyUser>(data);上面代码中的“数据”是你的json文件数据。2) 如果您的 json 是一个对象数组,例如 MyUser 数组。{&nbsp; "Users": [&nbsp; &nbsp; { "Id": 1, "Name": "Andrew", "Description": "Software Engineer"},&nbsp; &nbsp; { "Id": 2, "Name": "Eddy", "Description": "Software Developer"},&nbsp; &nbsp; { "Id": 3, "Name": "Matthew", "Description": "Web Developer"}&nbsp; ]}那么你需要一个 IEnumerable 类型属性 MyUser 的视图模型来反序列化public class MyUserVM{&nbsp; &nbsp; public IEnumerable<MyUser> Users { get; set; }}然后你需要使用上面的视图模型反序列化MyUserVM users = JsonConvert.DeserializeObject<MyUserVM>(data);上面代码中的“数据”是你的json文件数据。Q) 为什么这里需要视图模型?A) 因为 json 是一种基于文本的键值格式数据,您需要一个键才能在 JsonConvert 类中读取它。编辑:你请改变你的json键,如下所示{&nbsp; "name": "xx",&nbsp; "place": {&nbsp; &nbsp; "native": "aa",&nbsp; &nbsp; "school": "ee",&nbsp; &nbsp; "college": "dd"&nbsp; },&nbsp; "dob": "ss",&nbsp; "ids": [&nbsp; &nbsp; 0,&nbsp; &nbsp; 1,&nbsp; &nbsp; 2,&nbsp; &nbsp; 3,&nbsp; &nbsp; 4,&nbsp; &nbsp; 5,&nbsp; &nbsp; 6,&nbsp; &nbsp; 7,&nbsp; &nbsp; 8,&nbsp; &nbsp; 9,&nbsp; &nbsp; 10,&nbsp; &nbsp; 11,&nbsp; &nbsp; 12,&nbsp; &nbsp; 13,&nbsp; &nbsp; 14,&nbsp; &nbsp; 15,&nbsp; &nbsp; 16,&nbsp; &nbsp; 17,&nbsp; &nbsp; 18,&nbsp; &nbsp; 19,&nbsp; &nbsp; 20,&nbsp; &nbsp; 21,&nbsp; &nbsp; 22,&nbsp; &nbsp; 23,&nbsp; &nbsp; 24,&nbsp; &nbsp; 25,&nbsp; &nbsp; 26,&nbsp; &nbsp; 27,&nbsp; &nbsp; 28,&nbsp; &nbsp; 29,&nbsp; &nbsp; 30,&nbsp; &nbsp; 31,&nbsp; &nbsp; 32,&nbsp; &nbsp; 33,&nbsp; &nbsp; 34,&nbsp; &nbsp; 35,&nbsp; &nbsp; 36,&nbsp; &nbsp; 37,&nbsp; &nbsp; 38,&nbsp; &nbsp; 39,&nbsp; &nbsp; 40,&nbsp; &nbsp; 41,&nbsp; &nbsp; 42,&nbsp; &nbsp; 43,&nbsp; &nbsp; 44,&nbsp; &nbsp; 45,&nbsp; &nbsp; 46,&nbsp; &nbsp; 47,&nbsp; &nbsp; 48,&nbsp; &nbsp; 49,&nbsp; &nbsp; 50,&nbsp; &nbsp; 51,&nbsp; &nbsp; 52,&nbsp; &nbsp; 53,&nbsp; &nbsp; 54,&nbsp; &nbsp; 55,&nbsp; &nbsp; 56,&nbsp; &nbsp; 57,&nbsp; &nbsp; 58,&nbsp; &nbsp; 59,&nbsp; &nbsp; 60,&nbsp; &nbsp; 61,&nbsp; &nbsp; 62,&nbsp; &nbsp; 63,&nbsp; &nbsp; 64,&nbsp; &nbsp; 65,&nbsp; &nbsp; 66,&nbsp; &nbsp; 67,&nbsp; &nbsp; 68,&nbsp; &nbsp; 69,&nbsp; &nbsp; 70,&nbsp; &nbsp; 71,&nbsp; &nbsp; 72,&nbsp; &nbsp; 73,&nbsp; &nbsp; 74,&nbsp; &nbsp; 75,&nbsp; &nbsp; 76,&nbsp; &nbsp; 77,&nbsp; &nbsp; 78,&nbsp; &nbsp; 79,&nbsp; &nbsp; 80,&nbsp; &nbsp; 81,&nbsp; &nbsp; 82,&nbsp; &nbsp; 83,&nbsp; &nbsp; 84,&nbsp; &nbsp; 85,&nbsp; &nbsp; 86,&nbsp; &nbsp; 87,&nbsp; &nbsp; 88,&nbsp; &nbsp; 89,&nbsp; &nbsp; 90,&nbsp; &nbsp; 91,&nbsp; &nbsp; 92,&nbsp; &nbsp; 93,&nbsp; &nbsp; 94,&nbsp; &nbsp; 95,&nbsp; &nbsp; 96,&nbsp; &nbsp; 97,&nbsp; &nbsp; 98,&nbsp; &nbsp; 99,&nbsp; &nbsp; 100,&nbsp; &nbsp; 101,&nbsp; &nbsp; 102,&nbsp; &nbsp; 103,&nbsp; &nbsp; 104,&nbsp; &nbsp; 105,&nbsp; &nbsp; 106,&nbsp; &nbsp; 107,&nbsp; &nbsp; 108,&nbsp; &nbsp; 109,&nbsp; &nbsp; 110,&nbsp; &nbsp; 111,&nbsp; &nbsp; 112,&nbsp; &nbsp; 113,&nbsp; &nbsp; 114,&nbsp; &nbsp; 115,&nbsp; &nbsp; 116,&nbsp; &nbsp; 117,&nbsp; &nbsp; 118,&nbsp; &nbsp; 119,&nbsp; &nbsp; 120&nbsp; ],&nbsp; "mark": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "tenthmark": "345",&nbsp; &nbsp; &nbsp; "twelthmark": "1100"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Diploma": "G",&nbsp; &nbsp; &nbsp; "DiplomaPG": "R"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "Ug": "D",&nbsp; &nbsp; &nbsp; "PG": "E"&nbsp; &nbsp; }&nbsp; ]}并使用我为您创建的以下视图模型public class Place{&nbsp; &nbsp; public string native { get; set; }&nbsp; &nbsp; public string school { get; set; }&nbsp; &nbsp; public string college { get; set; }}public class Mark{&nbsp; &nbsp; public string tenthmark { get; set; }&nbsp; &nbsp; public string twelthmark { get; set; }&nbsp; &nbsp; public string Diploma { get; set; }&nbsp; &nbsp; public string DiplomaPG { get; set; }&nbsp; &nbsp; public string Ug { get; set; }&nbsp; &nbsp; public string PG { get; set; }}public class RootObject{&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public Place place { get; set; }&nbsp; &nbsp; public string dob { get; set; }&nbsp; &nbsp; public List<int> ids { get; set; }&nbsp; &nbsp; public List<Mark> mark { get; set; }}然后使用反序列化RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(data);

婷婷同学_

您不能只是反序列化为 IEnumerable...您需要一个模型来放入数据。您需要一个反映您的 JSON 结构的 ViewModel,然后您可以轻松地反序列化为您的 ViewModel 类型的 IEnumerable。JsonConvert.DeserializeObject<IEnumerable<SomeViewModel>>(jsonData)

森栏

您不能反序列化为接口,只能反序列化为具体类型,例如 List of T
打开App,查看更多内容
随时随地看视频慕课网APP