且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

序列化和反序列化json,无法反序列化当前的JSON对象

更新时间:2021-11-04 17:39:17

您有一个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的 Content 属性括在方括号之间的变量。尝试将此值存储在变量中,在该行上放置断点,然后启动调试会话;你会看到真正提供给Json构造函数的东西。

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);



您还可以查看 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.