VB.NET 将 DataTable 转换为 JSON 时出现问题
好的,我正在尝试使用 JavaScriptSerializer 来处理 this 代码。然而,当它到达最后一行时,它会崩溃;
Dim json As New String(sr.Serialize(dt))
我收到此错误消息;
检测到循环引用 序列化类型的对象时 'System.Reflection.Module'。
我真的很感激任何有助于解决这个问题的见解。
Ok so I'm trying to use the JavaScriptSerializer to work with this code. However it crashes when it reaches the last line;
Dim json As New String(sr.Serialize(dt))
I get this error message;
A circular reference was detected
while serializing an object of type
'System.Reflection.Module'.
I would really appreciate any insights that could help solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
循环引用意味着序列化对象将导致无限循环。
例如,如果您尝试序列化具有对对象“B”的 1 对 1 引用的对象“A”。
声明一个包含要使用 JSON 序列化的数据的类来解决此问题。
Circular reference means that serialising the object would result in an infinite loop.
For example if you would try to serialize object "A" having a 1 to 1 reference to object "B".
Declare a class containg the data you want to serialize with JSON to solve this issue.
正如 hkda150 已经说过的,您可以使用专门为序列化而定制的类。
此外,这将使您能够序列化外键值,而不是序列化相关的完整对象。因此,如果您要序列化对象 a,该对象具有 B 类型的属性 a.SomeB,那么您通常会希望 a.someB 的 ID 出现在您的网页中。显然,我的了解不够,无法判断这是否与您的特定用例相关。
顺便说一句,如果您发现自己在“业务对象”和“用于序列化的对象”之间进行了大量映射,您可能需要考虑使用 AutoMapper。
As hkda150 has already said, you could use a class specifically tailored for being serialized.
This will furthermore enable you to have foreign key values serialized instead of having related full objects serialized. Thus, if you are serializing object a which has a property a.SomeB of type B, then you will often want the ID of a.someB to be present in your webpage. Obviously I don't know enough to be able to say if this is relevant in your specific use case.
BTW, If you find yourself doing a lot of mapping between "business objects" and "objects meant for serialization" you may want to consider using AutoMapper.