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

由网友(我这么帅当然是女孩子)分享简介:阵列的序列返回以下的JSON:[{code:AAAA,说明:AAAA说明},{code:BBBB,说明:描述的BBBB}]我的目标是返回以下JSON:{AAAA:{说明:AAAA说明},BBBB:{说明:BBBB说明}}解决方案 您能够有所成就的这类似于(不完全一样的,你期待),如果不是的序列化阵列,建立一个临时的字典...

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

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

我的目标是返回以下JSON:

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

解决方案 JSON JSON.stringify JSON.parse

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

  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.

阅读全文

相关推荐

最新文章