如何编辑json数据运行时?
我想将数据添加到我的 ASP.NET MVC 应用程序中现有生成的 json 数据中,我必须做什么? 意思是,假设我有一个类公司,其部门列表作为财产。但是json序列化不能够支持这种循环引用。所以我想如果我首先序列化没有部门列表的公司对象,然后获取每个公司的部门,然后序列化此列表并将数据附加到公司序列化数据中。我知道这可能是错误的方式。但由于时间的最后通牒我必须这么做。请指导。
i want to add the data to existing generated json data in my asp.net mvc application , what i have to do ?
means , suppose i have class Company that having list of Departments as property. but json serialization is not able to support such kind of circular reference. so i thought how if, i get serialize the Company object first without list of Departments, then get departments for each company and then serialize this list and append data to company serialized data. i know this may be wrong way . but i have to do because of time ultimatum. please guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以定义一个没有任何循环引用的视图模型:
然后您可以在模型和视图模型之间进行映射(您可以使用 AutoMapper 为此),最后将视图模型返回到视图。
例如:
现在您不再有循环引用,您将能够成功地将视图模型序列化为 JSON,并且您只需传递视图所需的信息。
You could define a view model where you won't have any circular references:
and then you would map between your model and view model (you could use AutoMapper for this) and finally return the view model to the view.
For example:
Now you no longer would have circular references and you will be able to successfully serialize the view model to JSON and you would only pass the information that is required to the view.