Rajet Veshin

Rajet Veshin

  • NA
  • 2
  • 5.9k

Double back slashes inserted on receiving JSON(HttpClient)

May 20 2018 3:46 AM
I am new to programming and specifically new to .Net MVC(using .net core 2). I am having a controller method as below
  1. public async Task<JsonResult> GetCountriesList(string searchText)  
  2. {  
  3. HttpResponseMessage responseMessage;  
  4. string responseContent = string.Empty;  
  5. List<Country> listCountries = null;  
  6. using (var client = getHttpClient(WebClientContentType.JSON))  
  7. {  
  8. responseMessage = await client.GetAsync(new Uri(string.Format("{0}{1}", _webApiUrl, "/api/Country/GetCountryList")));  
  9. if (responseMessage.IsSuccessStatusCode)  
  10. {  
  11. responseContent = responseMessage.Content.ReadAsStringAsync().Result;  
  12. listCountries =(List<Country>)JsonConvert.DeserializeObject(responseContent, typeof(List<Country>));  
  13. if (!string.IsNullOrEmpty(searchText))  
  14. {  
  15. listCountries = listCountries.Where(m => m.ShortName.ToLower().StartsWith(searchText.ToLower())).ToList();  
  16. }  
  17. }  
  18. else  
  19. {  
  20. return Json(responseMessage.StatusCode);  
  21. }  
  22. }  
  23. return Json(listCountries);  
  24. }  
Here in "responseContent", i am getting extra slashes in my JSON
 
But in the API method, i am getting properly formatted JSON string My API method is
  1. [HttpGet] [Route("api/Country/GetCountryList")]  
  2. public IActionResult GetCountryList()  
  3. {  
  4. ProcedureReturnType mediaType = GetMediaType();  
  5. try  
  6. {  
  7. CountryHelper countryHelper = new CountryHelper();  
  8. var result = countryHelper.GetCountries(mediaType);  
  9. return Ok(result);  
  10. }  
  11. // end try  
  12. catch (Exception ex)  
  13. {  
  14. return BadRequest(ex.Message);  
  15. // end catch  
  16. // end method  
Any way, i can solve this issue as i need the list of countries which i expect to get from the valid JSON in listCountries
 
For example if i write
 
var result = "\r"; return Ok(result);
 
Then the result sent is "\r", but on the receiving end at line
 
responseContent is having the value "\"\r\""

Answers (1)