2011-12-08 12 views
5

これはWeb上で、ASP WebサービスではなくWCF Webサービス用のソリューションがいくつか見つかりました。ASP WebサービスのJSONレスポンスからd:と__typeを削除する方法

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

を、私はそれを返す必要があります:

現在、私が言うJSONレスポンスを取り戻すんだ。ここ

{"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ライブラリを使用を使用することができます。

また、__typeビットを削除する方法については、How to not serialize the __type property on JSON objectsを参照してください。削除のために

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

:あなたはまたしてTypeことで、この動作を変更することができます。 –

-1

あなたが使用している場合ServiceStack.Text JSON Serializerする必要があります:

JsConfig.ExcludeTypeInfo = true; 

この機能は自動的にv2.28に戻って追加しましたが、上記のコードは、シリアライズの外にあることを保持しました。私は私の設定ではfalseに `automaticFormatSelectionEnabled`を設定しなければならなかったそう、私はXMLのresposneではなく、JSONの1になってしまった、そうでない場合、これは御馳走を働いた

JsConfig<Type>.ExcludeTypeInfo = true; 
関連する問題