JsonConvert.DeserializeObject and wrapper "d" in WCF

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

If I parse JsonConvert.DeserializeObject (answer) , where is the answer

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

I have a mistake:

After parsing a value an unexpected character was encoutered: a. Line 1, position 9.

If I change the answer to

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

I start work.

So how to parse these "d" complete JSON responses from WCF services? Is there a better way to parse JSON?

+3
source share
7 answers

Now I got rid of the d shell with Regex.Replace and fixed the JSON response with the appropriate structure

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

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

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

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

?

0

, <enableWebScript/> , <webHttp defaultOutgoingResponseFormat="Json"/>, json, no "d" no "__type"

+10

, enableWebScript -. , webHttp - "" JSON JSON ASP.NET AJAX.

+2

json -, http://httputility.net/json-to-csharp-vb-typescript-class.aspx. json-, ( VB):

Public Class MyClass
     Public Property D As String
End Class

json . D , json, . , , D -, , !

+1

WebHttpBehavior, , , , Wrapped. :

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

, !

0
source

You may have a deserialization shell class that has one property called "d". After you have successfully deserialized, then get the value from property d.

0
source

Perhaps this helps.

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 only 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
source

Source: https://habr.com/ru/post/1721095/


All Articles