Сериализации и десериализации данных JSON, не может десериализовать JSON-объект текущего
Привет,
Я получил сообщение об ошибке, что мне нужно десериализовать json, поэтому я это сделал:
var user = db.Users.Select(x => new RegisterBindingModel() { Id = x.Id, Email = x.Email, UserName = x.UserName, FirstName = x.FirstName, LastName = x.LastName, Age = x.Age }); var a = user.ToList(); var b = Json(new { Data = a, Total = a.Count() }); List<RegisterBindingModel> c = JsonConvert.DeserializeObject<List<RegisterBindingModel>>(b.Content);
Что происходит из-за ошибки, так как формат json неверен, и я сделал следующее:
var user = db.Users.Select(x => new RegisterBindingModel() { Id = x.Id, Email = x.Email, UserName = x.UserName, FirstName = x.FirstName, LastName = x.LastName, Age = x.Age }); var a = user.ToList(); var b = Json(new { Data = a, Total = a.Count() }); string strserialize = JsonConvert.SerializeObject(b.Content); //var recordObject = JObject.Parse(b.Content); var v = b.Content.ToString(); List<RegisterBindingModel> c = JsonConvert.DeserializeObject<List<RegisterBindingModel>>(strserialize);
И я получил первую ошибку, с которой столкнулся:
>>
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Project.Models.RegisterBindingModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Data', line 1, position 8.'
Вот объект json:
{"Data":[{"Id":1,"Email":"AdminTest@gmail.com","UserName":"AdminTest", "FirstName":"Admin","LastName":"Test","Age":23}],"Total":13}
Что я уже пробовал:
string strserialize = JsonConvert.SerializeObject("["+b.Content+"]");
Что сделало формат json таким:
"[{ Data = System.Collections.Generic.List`1[Project.Models.RegisterBindingModel], Total = 13 }]"
Я получил эту ошибку:
Newtonsoft.Json.JsonSerializationException: 'Error converting value "[{ Data = System.Collections.Generic.List`1[Project.Models.RegisterBindingModel], Total = 13 }]" to type 'System.Collections.Generic.List`1[Project.Models.RegisterBindingModel]'. Path '', line 1, position 97.'
Также попробовал это сделать:
var c = JsonConvert.DeserializeObject <IDictionary<string, RegisterBindingModel>> (strserialize);
И я получил:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Project.Models.RegisterBindingModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'Data', line 1, position 9.'