Can't convert returned string from service to int in c#

I create a service as you can see :

public int ValidateAndSubmitReception(NajaResult ReceptionData)
{
    ClientRequest.Headers["Content-type"] = "application/json";
    serializerToUplaod = new DataContractJsonSerializer(typeof(NajaResult));
    serializerToUplaod.WriteObject(ms, ReceptionData);
    string Result = System.Text.Encoding.UTF8.GetString(ClientRequest.UploadData(ServiceHostName + "/ReceptionService.svc/ValidateAndSubmitReception", "POST", ms.ToArray()));
    return int.Parse(Result.ToString());
    //return receptionid

}

My service returns "\"-1\"" but when i click on magnifier it shows me -1, so when i want to convert to int it throw a convert exception .why ?

enter image description here

Here my service interface

   [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/ValidateAndSubmitReception", RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]

        string ValidateAndSubmitReception(NajaResult NajaResult);
Jon Skeet
people
quotationmark

Firstly, ignore the backslashes in the debugger. They're misleading. The string you've got is:

"-1"

That's the actual text of the string, as you'd see it if you printed it to the console.

While you could just remove the quotes manually, I would personally take a different tack: it looks like the service is returning JSON, so use a JSON parser to parse the token... and to perform the subsequent int conversion:

using System;
using Newtonsoft.Json.Linq;

class Test
{
    static void Main(string[] args)
    {
        string response = "\"-1\"";
        JToken token = JToken.Parse(response);
        int value = (int) token;
        Console.WriteLine(value);
    }
}

That way if the service ever returns a value which is perfectly valid JSON, but isn't just a decimal number wrapped in quotes, your code will still cope. For example, support the service decides (validly, but unusually) to escape the '-' character, like this:

"\u002d1"

This code will be fine with that - it will unescape the string because it's treating it as JSON.

people

See more on this question at Stackoverflow