数组的序列化返回以下 JSON:
[{"Code":"AAAA","Description":"Description of AAAA"},{"Code":"BBBB","Description":"Description of BBBB"}]
我的目标是返回以下 JSON:
{"AAAA":{"Description":"Description of AAAA"},"BBBB":{"Description":"Description of BBBB"}}
请您参考如下方法:
如果不是序列化数组,而是构建一个临时字典并序列化它,您可以实现一些类似(与您期望的不完全相同)。
var dict = new Dictionary<String, YourClass>();
foreach (YourClass yourObj in listOfObjs)
{
dict[yourObj.Code] = yourObj;
}
// then serialize "dict"
您可以向您的 JSON 序列化器添加一条规则,使其避免序列化 YourClass 中的“代码”属性,并且您最终会得到一个 完全 的 JSON 对象在您的示例中显示。
