JsonConvert.DeserializeObject and “d” wrapper in WCF

后端 未结 7 466
梦谈多话
梦谈多话 2020-12-21 08:53

By default WCF service wrap JSON response in \"d\" wrapper and there I found a problem with parsing it.

If I parse with JsonConvert.DeserializeObject(respons

相关标签:
7条回答
  • 2020-12-21 09:07

    I'm assuming you are using <enableWebScript/> in your behavior config, replace that with <webHttp defaultOutgoingResponseFormat="Json"/> and you will get nice and clean json, no "d" and no "__type"

    0 讨论(0)
  • 2020-12-21 09:07

    Looks like you're using the enableWebScript behavior on your webHttpBinding. You should probably be using the webHttp behavior instead- this gives you "clean" JSON instead of the ASP.NET AJAX client JSON.

    0 讨论(0)
  • 2020-12-21 09:07

    You could have a deserialization wrapper class that has one property called "d". Once you've successfully deserialized to it then get the value from the d property.

    0 讨论(0)
  • 2020-12-21 09:15

    If you are switching to WebHttpBehavior and you still get an error message about the body elements not being wrapped, manually set the body style of the methods you're dealing with to Wrapped. Do it like so:

    [OperationContract(BodyStyle = WebMessageBodyStyle.Wrapped, ...)] string DoSomething(...)

    Hope this helps!

    0 讨论(0)
  • 2020-12-21 09:18

    Maybe this helps.

    The service:

    namespace Application.Service
    {
            [ServiceBehavior(UseSynchronizationContext = false,
            ConcurrencyMode = ConcurrencyMode.Multiple,
            InstanceContextMode = InstanceContextMode.PerCall),
            AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
            public class VendorService : IVendorService
            {
              public List<Vendor> RetrieveMultiple(int start, int limit, string sort, string dir)
              {
                 //I don't do any manual serialization
                 return new Vendor();
              }
            }
    }
    

    The contract:

        [ServiceContract(Namespace = "Application.Service.Contracts")]            
        public interface IVendorService
        {
           [OperationContract]
           [WebInvoke(ResponseFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
            List<Vendor> RetrieveMultiple(int start, int limit, string sort, string dir);
         }
    

    My Svc file has just this line:

    <%@ ServiceHost Service="Application.Service.VendorService" %>
    

    Web.config

    <system.serviceModel>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        <behaviors>
          <endpointBehaviors>
            <behavior name="jsonBehavior">
              <enableWebScript />
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="DefaultServiceBehavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
      <service behaviorConfiguration="DefaultServiceBehavior" name="Application.Service.VendorService">
        <endpoint behaviorConfiguration="jsonBehavior" address="" binding="webHttpBinding" contract="Application.Service.Contracts.IVendor" />
      </service>
    
    0 讨论(0)
  • 2020-12-21 09:19

    Now I got rid of "d" wrapper with Regex.Replace and fix JSON response with proper structure

    {\"Guid\":\"a0b70d2f-7fe4-4aa2-b600-066201eab82d\",\"Name\":\"Thelma\"}
    {\"Guid\":\"d56d4d4f-6029-40df-a23b-de27617a1e43\",\"Name\":\"Lousie\"}\"}
    

    I also make a class with Guid and Name, defined as string in it.

    Then try to deserialize it with

    List<myStruct> o = JsonConvert.DeserializeObject<List<myStruct>>(response);
    

    But i get an error

    Expected a JsonObjectContract or JsonDictionaryContract for type 'System.Collections.Generic.List`1[mynamespace.myStruct]', got 'Newtonsoft.Json.Serialization.JsonArrayContract'.
    

    Where is the trick?

    0 讨论(0)
提交回复
热议问题