简体   繁体   English

无法在C#中将返回的字符串从服务转换为int

[英]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 ? 我的服务返回"\\"-1\\""但是当我单击放大镜时,它会向我显示-1,因此当我要将其转换为int时,它将引发转换异常。为什么?

在此处输入图片说明

Here my service interface 这是我的服务界面

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

        string ValidateAndSubmitReception(NajaResult NajaResult);

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: 虽然您可以手动删除引号,但我个人会采取另一种做法:看起来该服务正在返回JSON,因此请使用JSON解析器解析令牌...并执行后续的int转换:

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. 如果服务曾经返回一个值,这是完全有效的JSON,但只是包裹在引号的十进制数,这样,你的代码仍然可以应付。 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 . 这段代码就可以了-将字符串转义, 因为它将其视为JSON

Problem: Your string contains quotation mark ( " ) in it so it wouldn't be parsed the way your are trying to parse it. 问题:您的字符串中包含引号( " ),因此不会像尝试解析该字符串那样对其进行解析。

Solution: You should remove the quotation mark ( " ) first using the String.Replace method and then try to parse the string. 解决方案:您应该首先使用String.Replace方法删除引号( " ),然后尝试解析该字符串。

Code: You may need to replace the int.parse statement with the following: 代码:您可能需要将int.parse语句替换为以下内容:

return int.Parse(Result.Replace("\"", string.Empty));

Your Result string returns the integer inside of quotation marks. 您的Result字符串返回引号内的整数。 Remove them, and try to parse it again. 删除它们,然后尝试再次解析它。

string Result = System.Text.Encoding.UTF8.GetString(ClientRequest.UploadData(ServiceHostName + "/ReceptionService.svc/ValidateAndSubmitReception", "POST", ms.ToArray()));
Result = Result.Replace("\"", string.Empty);
return int.Parse(Result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM