Reputation: 109
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.
Upvotes: 4
Views: 7255
Reputation: 578
// 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);
}
}
Upvotes: 1