3

I have a http invokable wcf service method;

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/CheckHMAC/{hMac}")]
public string CheckId(string Id)
{
    Result result = new Result() { OTP = 1, IsSuccessful = false, CustomerId = "" };
    return JsonConvert.SerializeObject(result);
}

This method produces an output like;

"{\"IsSuccessful\":false,\"OTP\":1,\"CustomerId\":\"\"}"

The client which uses this method complains this format since it is not valid, i ve tested it with another client and yes it seems not valid. Until now, i have never had a problem like this, the output should be easily deserialized, why json object wrapped with double quotes? How can i get a valid json string?

{"IsSuccessful":false,"OTP":1,"CustomerId":""}

ibubi
  • 2,469
  • 3
  • 29
  • 50

1 Answers1

5

Well, it seems i missed this post despite a long search. What is wrong here is the method signature: string;

..the API controller will serialize the string as a JavaScript string literal—which will cause the string to be wrapped in double quotes and cause any other special characters inside the string to escaped with a backslash.

So, just returning the object itself, makes json data valid for the client.

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/CheckHMAC/{hMac}")]
public Result CheckId(string Id)
        {
            Result result = new Result() { OTP = 1, IsSuccessful = false, CustomerId = "" };
            return result;
        }
Community
  • 1
  • 1
ibubi
  • 2,469
  • 3
  • 29
  • 50