I'm creating a web service that generates random personal data (hence the inclusion of a counter in the code below) using a combination of a third party web service and methods. However, when I specified the content type as
application/json
, the result contains escape backslashes.
From what I've read, this is probably a result of the serialization used but despite trying several solutions, I can't solve the issue.
Data Model
public class Response
{
public string name { get; set; }
public string surname { get; set; }
public string address { get; set; }
public string telephone { get; set; }
public string email { get; set; }
public string dateOfBirth { get; set; }
public string sex { get; set; }
public string countryOfBirth { get; set; }
public string currency { get; set; }
}
Method
public class DatasetServices
{
public string GenerateDataset()
{
int counter = 3;
StringBuilder result = new StringBuilder();
for (int i = 0; i < counter; i++)
{
try
{
Dataset dataset = new Dataset();
Dataset.Item item = new Dataset.Item();
string apiKey = "KEY";
HttpWebRequest apiRequest = WebRequest.Create("URI" + apiKey) as HttpWebRequest;
string apiResponse = "";
using (HttpWebResponse response = apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
Response responseObject = JsonConvert.DeserializeObject<Response>(apiResponse);
item.name = responseObject.name;
item.surname = responseObject.surname;
item.address = responseObject.address;
item.city = GenerateCity();
item.telephone = responseObject.telephone;
item.email = responseObject.email;
item.dateOfBirth = responseObject.dateOfBirth;
item.sex = responseObject.sex;
item.maritalStatus = GenerateMaritalStatus();
item.nationality = responseObject.countryOfBirth;
item.countryOfBirth = responseObject.countryOfBirth;
item.grossAnnualIncome = GenerateGrossAnnualIncome();
item.currency = responseObject.currency;
var json = JsonConvert.SerializeObject(item);
result.Append(json);
}
catch (Exception)
{
if (i == counter)
{
throw;
}
}
}
string output = result.ToString();
return output;
throw new ArgumentOutOfRangeException("The generation counter was equal to zero.");
}
Controller
[HttpGet]
public ActionResult<IEnumerable> OutputDataset()
{
Response.ContentType = "application/json";
return datasetServices.GenerateDataset();
}
Result
"{\"name\":\"Sibby\",\"surname\":\"Packman\",\"address\":\"59 Aberg Alley\",\"city\":\"San Javier\",\"telephone\":\"6322049027\",\"email\":\"spackman0@chicagotribune.com\",\"dateOfBirth\":\"10/22/1987\",\"sex\":\"Female\",\"maritalStatus\":\"Single\",\"nationality\":\"RU\",\"countryOfBirth\":\"RU\",\"grossAnnualIncome\":71000,\"currency\":\"RUB\"}{\"name\":\"Mariska\",\"surname\":\"Worrell\",\"address\":\"927 Green Ridge Point\",\"city\":\"Frisco\",\"telephone\":\"5668115189\",\"email\":\"mworrell0@php.net\",\"dateOfBirth\":\"10/1/1998\",\"sex\":\"Female\",\"maritalStatus\":\"Single\",\"nationality\":\"GT\",\"countryOfBirth\":\"GT\",\"grossAnnualIncome\":24000,\"currency\":\"GTQ\"}{\"name\":\"Eleonore\",\"surname\":\"Follan\",\"address\":\"3876 Colorado Avenue\",\"city\":\"Mont-Dore\",\"telephone\":\"4004483706\",\"email\":\"efollan0@jalbum.net\",\"dateOfBirth\":\"3/6/1992\",\"sex\":\"Female\",\"maritalStatus\":\"Married\",\"nationality\":\"CN\",\"countryOfBirth\":\"CN\",\"grossAnnualIncome\":50000,\"currency\":\"CNY\"}"
>` from `OutputDataset()` and let the framework do the work.