gpt4 book ai didi

c# - asp.net webservices - 如何返回与 "d"相同级别的参数

转载 作者:太空宇宙 更新时间:2023-11-03 13:52:14 25 4
gpt4 key购买 nike

自 .NET 3.5 起,返回 json 的 Web 服务将数据包装在名为“d”的参数中。我描述的功能已记录在案 here在其他地方。

我想知道是否有办法向 json 添加一个与“d”处于同一级别的参数。

所以借用上面的例子,如果我的一个网络服务的输出是

{"d":{"__type"    : "Person",
"FirstName" : "Dave",
"LastName" : "Ward"}}

我想要的是

{"d":{"__type"    : "Person",
"FirstName" : "Dave",
"LastName" : "Ward"},
"z":{"__type" : "AnotherType",
"Property" : "Value"}}

有没有办法做到这一点?

最佳答案

尽管无论如何都不推荐这样做。 JSON 结果被包装为一项安全功能。

但是,如果您绝对需要,这里有一个解决方案:

在您需要更改元素的[WebMethod]中添加

        Context.Response.ClearContent();
Context.Response.Filter = new JsonHackFilter(Context.Response.Filter);

JsonHackFilter 在哪里

class JsonHackFilter : MemoryStream
{
private readonly Stream _outputStream = null;

public JsonHackFilter(Stream output)
{
_outputStream = output;
}

public override void Write(byte[] buffer, int offset, int count)
{

string bufferContent = Encoding.UTF8.GetString(buffer);

// TODO: Manually manipulate the string here

_outputStream.Write(Encoding.UTF8.GetBytes(bufferContent), offset,
Encoding.UTF8.GetByteCount(bufferContent));

base.Write(buffer, offset, count);
}

}

关于c# - asp.net webservices - 如何返回与 "d"相同级别的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13402711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com