Jay Raj Mishra
Jay Raj Mishra

Reputation: 140

unable to remove the slash "\" in json string in c#

I am unable to remove the slash while requesting to the Post API in c#.By default slash is added in the value, is there is a way to remove the slash in the string.I am sending the string array to api.I have used replace also but it is not working.

"[\"9782163865630.jpg\",\"9946239664158.jpg\",\"9946237403166.jpg\",\"10056487272478.jpg\",\"10056486322206.jpg\",\"10060074352670.jpg\",\"9999843459102.jpg\",\"9716071170078.jpg\",\"9716071497758.jpg\",\"10052987715614.jpg\",\"10052985683998.jpg\",\"10056390115358.jpg\",\"10056391622686.jpg\",\"10056391360542.jpg\",\"9837103120414.jpg\",\"9837102923806.jpg\",\"9837104857118.jpg\"]"

    public void PostWebAPI(List<string> FileNameList)
    {
        string json = JsonConvert.SerializeObject(FileNameList).ToString();
        json = json.Replace(@"\","");

        var client = new RestClient("eg.api.stackflow.com/post");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AlwaysMultipartFormData = true;
        request.AddParameter("filePaths", json);
        request.AddParameter("bucketAsDir", "false");
        IRestResponse response = client.Execute(request);
    }

Visual Studio debugging:

enter image description here

enter image description here

Upvotes: 2

Views: 6675

Answers (4)

Jay Raj Mishra
Jay Raj Mishra

Reputation: 140

I was facing the problem with the above code using RestClient instead of it, I have used HttpClient, Now from API I am not getting the error. Slash is added to request paramater using RestClient but in HttpClient it is not added, due to this UnicodeEncoding.UTF8, "application/json" the actuall value is been passed in the parameter of the API.

  public async Task CallAPIAsync(List<string> objFileNameList)
    {

        var Info = new APIModel
        {
            filePaths = objFileNameList,
            bucketAsDir = "false"
        };

        string request = JsonConvert.SerializeObject(Info);

        using (var client = new HttpClient())
        {
            client.Timeout = Timeout.InfiniteTimeSpan;
            var stringContent = new StringContent(request, UnicodeEncoding.UTF8, "application/json");
            client.BaseAddress = new Uri("eg.api.stackflow.com/post");

            var response = await client.PostAsync("post", stringContent);
            var message = response.Content.ReadAsStringAsync().Result;
        }
    }

Upvotes: 0

InBetween
InBetween

Reputation: 32750

The backslash \ is not a readable character in your string, its an escape character for the double quotes: \"; its telling the compiler that the " following the backslash is not a string delimiter but a regular character part of the string.

Consider you want to have a string that contains the following text: "Hello" (not Hello). You would write the following:

string s = "\"Hello\"";

s is really "Hello" but the debugger will show it as "\"Hello\"" because it has no better way to desambiguate to the user " as string delimiter from " as part of the string itself.

In short, the escape character \ inside a string tells the compiler that the following character is used in a way that is not the default interpretation the compiler would consider. Other expamples:

  • \": a regular double quote instead of the string delimiter "
  • \0: null charater instead of a regular 0
  • \n: new line character instead of a regular n
  • \t: tab character instead of a regular t
  • \\: backslash instead of the escape character \
  • etc.

Check here for the whole list.

So, to make a long story short, dont worry, your string really is: ["9782163865630.jpg","9946239664158.jpg","9946237403166.jpg",.... You can verify this by simply printing out the string to the console: Console.WriteLine(json);

Upvotes: 6

Nguyen Van Thanh
Nguyen Van Thanh

Reputation: 825

Each quotes " is a special symbol in C#. Those backslashes \ just escape sequences for the quotes ". It dont make your result error. Try to write this in Visual Studio:

string myString = "This is my "string""; // Error

You can use a backslash before each quote (\") to fix it:

string myString = "This is my \"string\""; // This work well

Try this here

Upvotes: 0

Youssef13
Youssef13

Reputation: 4944

The slash isn't actually in the string. You're trying to remove something that doesn't exist. The debugger is just escaping the double quotes. Click on the magnifier icon will get you some options on how the debugger displays it.

Upvotes: 0

Related Questions