【问题标题】:Modifying JSON output修改 JSON 输出
【发布时间】: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),然后是后面的所有项目。

我重写了JsonMediaTypeFormatterWriteToStreamAsync 方法

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


    【解决方案1】:

    如果您想在格式化程序本身上执行此操作,您可能需要将包装代码写入writeStream,如下面的代码所示(在记事本中测试):

    public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        if ((typeof(IEnumerable<AttributeCollection>).IsAssignableFrom(type)))
        {
            var list = (IEnumerable<AttributeCollection>)value;
            byte[] headerBytes = Encoding.UTF8.GetBytes("{\"allAttributes\":");
            byte[] footerBytes = Encoding.UTF8.GetBytes("}");
            writeStream.Write(headerBytes, 0, headerBytes.Length);
            foreach (var item in list)
            {
                await base.WriteToStreamAsync(item.GetType(), item, writeStream, content, transportContext);
            }
    
            writeStream.Write(footerBytes, 0, footerBytes.Length);
        }
        else
        {
            return await base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
        }
    }
    

    另一种选择是创建一个包装类并对其进行序列化:

    public class MyWrappingClass
    {
        public IEnumerable<AttributeCollection> allAttributes { get; set; }
    }
    
    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        if ((typeof(IEnumerable<AttributeCollection>).IsAssignableFrom(type)))
        {
            var list = (IEnumerable<AttributeCollection>)value;
            var obj = new MyWrappingClass { allAttributes = list };
            return base.WriteToStreamAsync(obj.GetType(), obj, writeStream, content, transportContext);
        }
        else
        {
            return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2017-01-30
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多