序列化数组使用.NET JSON对象

编程入门 行业动态 更新时间:2024-10-25 16:24:31
本文介绍了序列化数组使用.NET JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

阵列的序列返回以下的JSON:

[{code:AAAA,说明:AAAA说明},{code:BBBB,说明:描述的BBBB}]

我的目标是返回以下JSON:

{AAAA:{说明:AAAA说明},BBBB:{说明:BBBB说明}}

解决方案

您能够有所成就的这类似于(不完全一样的,你期待),如果不是的序列化阵列,建立一个临时的字典和序列化。

VAR字典=新字典<字符串,YourClass>(); 的foreach(YourClass yourObj在listOfObjs) {     字典[yourObj code] = yourObj; } //然后序列化字典

您可以添加一条规则,你的JSON序列化,使之避免序列化的 YourClass code的属性,你会最终有一个JSON对象完全作为您在您的示例显示。

The serialization of the array returns the following JSON:

[{"Code":"AAAA","Description":"Description of AAAA"},{"Code":"BBBB","Description":"Description of BBBB"}]

My goal is to return the following JSON:

{"AAAA":{"Description":"Description of AAAA"},"BBBB":{"Description":"Description of BBBB"}}

解决方案

You can achieve something simliar (not exactly the same you are expecting) if instead of serializing an array, build a temporary Dictionary and serialize it.

var dict = new Dictionary<String, YourClass>(); foreach (YourClass yourObj in listOfObjs) { dict[yourObj.Code] = yourObj; } // then serialize "dict"

You could add a rule to your JSON serializer to make it avoid serializing "code" property in YourClass, and you will end up with a JSON object exactly as you show in your example.

更多推荐

序列化数组使用.NET JSON对象

本文发布于:2023-11-26 17:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   序列化   NET   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!