gpt4 book ai didi

asp.net - 如何删除 d : and __type from JSON response for ASP web service

转载 作者:行者123 更新时间:2023-12-03 03:20:50 27 4
gpt4 key购买 nike

我在网络上找到了几种针对 WCF Web 服务而不是 ASP Web 服务的解决方案。

目前,我收到了一个 JSON 响应,内容如下:

{"d":[{"__type":"NetworkFuzzWebSvc.Sessions","BaseUri":"http://localbox","SessionId":"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

我需要它返回:

{"Sessions":["BaseUri":"http://localbox","SessionId":"43b8716f-40ab-43bf-8311-575c2ecd2730}]}

这是我正在使用的 Web 服务代码的副本 (NetFuzzWebSvc.asmx):

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace NetworkFuzzWebSvc
{
public class Sessions
{
public string BaseUri;
public string SessionId;
}

/// <summary>
/// Summary description for NetFuzzJson
/// </summary>
[WebService(Namespace = "http://localbox")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]
public class NetFuzzJson : WebService
{
List<Sessions> Sessions = new List<Sessions>
{
new Sessions{
BaseUri = "http://localbox/",
SessionId="43b8716f-40ab-43bf-8311-575c2ecd2730"
}
};

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Sessions> GetAllSessions()
{
return Sessions;
}
}

谁有办法解决这个问题吗?谢谢!

最佳答案

删除“d”和“__type”:

.svc

[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "POST",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<TestDTO> GetAll();
}

.config

<behaviors>
<endpointBehaviors>
<behavior name="DebugJSonBehavior" >
<enableWebScript />
<!--need set automaticFormatSelectionEnabled attribute -->
<webHttp automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DebugJSonBehavior" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

Js:

    $.ajax({
type: "POST",
url: _serviceUrl + "/TestService.svc/GetAll",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataret) { ... },
error: function (xmlHttpRequest, textStatus, errorThrown) {... },
complete: function () { ... }
});

关于asp.net - 如何删除 d : and __type from JSON response for ASP web service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8437472/

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