序列化和反序列化json,无法反序列化当前的JSON对象 [英] Serialize and deserialize json, cannot deserialize the current JSON object
本文介绍了序列化和反序列化json,无法反序列化当前的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到错误消息,我必须反序列化json,所以我做了:
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);
这是通过错误由于json格式不正确,我做了以下内容:
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.'
这是json对象:
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+"]");
其中json格式如下:
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.'
推荐答案
您有一个RegisterBindingModel对象列表,然后创建一个新对象使用指向该列表的Data属性和Total属性,然后尝试将该对象反序列化回列表。显然这不会起作用,因为新对象不是RegisterBindingModel的列表,它是一个具有Data属性和Count属性的对象。你需要创建一个新的对象来反序列化数据
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);
您是否见过并尝试过 Newtonsoft :序列化集合 [ ^ ]已经?
您的代码非常混乱,因为您要序列化b的
括在方括号之间的变量。尝试将此值存储在变量中,在该行上放置断点,然后启动调试会话;你会看到真正提供给Json构造函数的东西。Content
属性
Have you seen and tried Newtonsoft: Serializing Collections[^] already?
Your code is quite confusing because what you are serializing theContent
property of theb
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);
您还可以查看 v $ c $的值c>变量,就我所见,你似乎没有使用它。
You can also watch for the value of the v
variable, which you do not seem to use as far as I can see.
这篇关于序列化和反序列化json,无法反序列化当前的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文