Hi,
I got error message that I have to deserialize json, so I did:
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);
Which is through an error as json format is incorrect, and I did the following:
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 v = b.Content.ToString();
List<RegisterBindingModel> c =
JsonConvert.DeserializeObject<List<RegisterBindingModel>>(strserialize);
And I got the first error I faced:
>>
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.'
Here is json object:
{"Data":[{"Id":1,"Email":"AdminTest@gmail.com","UserName":"AdminTest",
"FirstName":"Admin","LastName":"Test","Age":23}],"Total":13}
What I have tried:
string strserialize = JsonConvert.SerializeObject("["+b.Content+"]");
Which made json format like this:
"[{ Data = System.Collections.Generic.List`1[Project.Models.RegisterBindingModel],
Total = 13 }]"
I got this error:
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.'
Also tried this:
var c = JsonConvert.DeserializeObject <IDictionary<string,
RegisterBindingModel>> (strserialize);
And I got:
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.'