【发布时间】:2014-05-28 21:26:48
【问题描述】:
我有一个返回 List<AttributeCollection> 的 Web API 方法。 AttributeCollection 显然是一种属性列表,其中包含每个属性的值(在本例中由 CRM 2011 SDK 获取)。
这是该方法返回的 JSON:
[
[
{
"Key": "mobilephone",
"Value": "(430) 565-1212"
},
{
"Key": "firstname",
"Value": "John"
}
],
[
{
"Key": "mobilephone",
"Value": "(430) 565-1313"
},
{
"Key": "firstname",
"Value": "Mark"
}
]
]
现在,第一对括号是列表的可视化表示,然后每个 AttributeCollection 都有多对括号 ([])。
我想去掉第一对括号,将其替换为顶级元素名称(即:allAttributes),然后是后面的所有项目。
我重写了JsonMediaTypeFormatter 的WriteToStreamAsync 方法
public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, HttpContent content, TransportContext transportContext)
{
if ((typeof(IEnumerable<AttributeCollection>).IsAssignableFrom(type)))
{
//anything I could do here ?
}
return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
}
我想操作 JSON 字符串本身,但似乎无法从那里访问。
有什么想法吗?
谢谢。
【问题讨论】:
标签: c# .net json asp.net-web-api