2011-12-08 30 views
5

我在Web上發現了幾個針對WCF Web服務而不是ASP Web服務的解決方案。如何從ASP Web服務的JSON響應中刪除d:和__type

目前,我取回一個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; 
     } 
    } 

任何人都有解決方案嗎? 謝謝!

回答

1

我不認爲你可以。我認爲這是返回的json的格式。

你可以嘗試擺脫ResponseFormat位,並返回一個字符串,並使用

JavaScriptSerializer ser = new JavaScriptSerializer(); 
string json = ser.Serialize(objectClass); 
return json; 

甚至更​​好使用JSON.Net庫。

另請參閱How to not serialize the __type property on JSON objects瞭解如何刪除__type位。

2

對於刪除 「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() { ... } 
    }); 
+1

我必須設置'automaticFormatSelectionEnabled'假在我的配置,否則我結束了一個XML resposne,而不是一個JSON一個,否則該工作一種享受:您還可以通過Type與更改此行爲。 –

-1

如果您在使用ServiceStack.Text JSON Serializer你只需要:

JsConfig.ExcludeTypeInfo = true; 

此功能在v2.28自動添加回來,但上面的代碼保留了這一點的系列化。

JsConfig<Type>.ExcludeTypeInfo = true; 
相關問題