Home > Back-end >  C# How to get rid of "\" from JSON?
C# How to get rid of "\" from JSON?

Time:05-09

I have following C# code which generates output like this:

    Task<OnlineResponse> task = client.Execute(query);
    OnlineResponse response = task.Result;
    Result result = response.Results[0];

    dynamic resultJson = JsonConvert.SerializeObject(result.Data);
        
    var x = Regex.Replace(resultJson.ToString(), @"[\[\]'] ", "");

    return x;

This is output:

"{\"GLDETAIL\":{\"RECORDNO\":\"264378-1756289-919567-- 
accrual\",\"BATCH_DATE\":\"02/01/2022\"}},
{\"GLDETAIL\":{\"RECORDNO\":\"264378-1756290-919568--   
accrual\",\"BATCH_DATE\":\"02/01/2022\"}}"

I am trying to get rid of all backslashes.

I applied "Regex.Replace", but it would not work.

This is expected output:

"{"GLDETAIL":{"RECORDNO":"264378-1756289-919567-- 
accrual","BATCH_DATE":"02/01/2022"}},
{"GLDETAIL":{"RECORDNO":"264378-1756290-919568--   
accrual","BATCH_DATE":"02/01/2022"}}"

CodePudding user response:

you serialized json string twice. just return the result.Data as it is. its already a json string. if you remove backslashes you wont be able deserilze the object.

  • Related