4

I am displaying some data in JSON format however, I have unwanted slashes in my output. These are my codes:

RestfulClient.cs

public class RestfulClient
{
    private static HttpClient client;
    private static string BASE_URL = "http://localhost:8080/";

    static RestfulClient()
    {
        client = new HttpClient();
        client.BaseAddress = new Uri(BASE_URL);
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task<string> addition(int firstNumber, int secondNumber)
    {
        try
        {
            var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);
            var response = await client.GetAsync(endpoint);
            return await response.Content.ReadAsStringAsync();
        }
        catch (Exception e)
        {
            HttpContext.Current.Server.Transfer("ErrorPage.html");
        }
        return null;
    }
}

AdditionController.cs

public class AdditionController : ApiController
{
    private RestfulClient restfulClient = new RestfulClient();
    public async Task<IHttpActionResult> Get(int firstNumber, int secondNumber)
    {
        var result = await restfulClient.addition(firstNumber, secondNumber);
        return Json(result);
    }
}

Output:

"{\"firstNumber\":9,\"secondNumber\":6,\"sum\":15}"

Expected output:

"{"firstNumber":9,"secondNumber":6,"sum":15}"

Do I have to deserialize the string to achieve that? If yes, how do i go about do that? Or do I have to change the application/json part? Someone please help me thank you so much in advance.

Susmitha Naidu
  • 109
  • 1
  • 12
  • Possible duplicate of [Replacing escape characteres from json in c#](https://stackoverflow.com/questions/16692371/replacing-escape-characteres-from-json-in-c-sharp) – Koby Douek Nov 02 '17 at 06:35
  • return Json(result).Replace(@"\", " "); this code is enough for handling this. – Sandeep Nov 02 '17 at 06:37
  • I get the error `JsonResult' does not contain a definition for 'Replace' and no extension method 'Replace' accepting a first argument of type 'JsonResult' could be found` – Susmitha Naidu Nov 02 '17 at 06:41
  • I run it on localhost – Susmitha Naidu Nov 02 '17 at 06:42
  • It looks like you're double-serializing a JSON string along the lines of [JSON.NET Parser *seems* to be double serializing my objects](https://stackoverflow.com/q/25559179). And since `result` is already serialized properly as JSON you can probably use the answer from [Return a JSON string explicitly from Asp.net WEBAPI](https://stackoverflow.com/q/17097841) in your `AdditionController.cs`. – dbc Nov 02 '17 at 06:43

1 Answers1

1

// Try This code Need Convert specific type

    public class Temp
    {
      public string firstNumber{ get;set;}
      public decimal secondNumber{ get;set;}
      public decimal sum{ get;set;}
    }


    public class AdditionController : ApiController
    {
     private RestfulClient restfulClient = new RestfulClient();
     public async Task<IHttpActionResult> Get(int firstNumber, int secondNumber)
     {
        var result = await restfulClient.addition(firstNumber, secondNumber);
        var resultDTO = JsonConvert.DeserializeObject<Temp>(result);
        return Json(resultDTO);
     }
    } 
Susmitha Naidu
  • 109
  • 1
  • 12
Sunny Jangid
  • 578
  • 4
  • 19