序列化数组使用.NET JSON对象 [英] Serialize an array into JSON objects using .NET
本文介绍了序列化数组使用.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对象< STRONG>完全作为您在您的示例显示。
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对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文