VB.NET problem converting DataTable to JSON

只谈情不闲聊 提交于 2019-12-24 06:34:00

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2307605/vb-net-problem-converting-datatable-to-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!