Click here to Skip to main content
14,698,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 recordObject = JObject.Parse(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.'
Posted
Updated 30-Apr-19 0:59am

You have a list of RegisterBindingModel objects, you then create a new object with a property of Data that points to that list and a Total property, you then try and deserialise that object back to a list. Obviously that's not going to work as the new object isn't a list of RegisterBindingModel, it's an object with a Data property and a Count property. You'll need to create a new object to deserialise the data to

public class MyData
{
    public List<RegisterBindingModel> Data { get; set; }
    public int Total { get; set; }
}


MyData c = JsonConvert.DeserializeObject<MyData>(b.Content);
   
Have you seen and tried Newtonsoft: Serializing Collections[^] already?

Your code is quite confusing because what you are serializing the Content property of the b variable enclosed between square brackets. Try storing this value in a variable, put a breakpoint on that line, and launch a debug session; you will see what is really provided to the Json constructor.
var content = b.Content; // Put a breakpoint on this line
string strserialize = JsonConvert.SerializeObject(content);

You can also watch for the value of the v variable, which you do not seem to use as far as I can see.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900