我正在尝试在我的前端使用数据,该数据调用API Broker并且此API Broker调用我的API。在我的前端,我得到的JSON数据返回JSON,里面有很多反斜杠。我该如何防止这种情况?看下面的代码和错误:
在我的前端使用我的API:
[HttpGet]
public async Task<ActionResult> getCall()
{
string url = "http://localhost:54857/";
string operation = "getClients";
using (var client = new HttpClient())
{
//get logged in userID
HttpContext context = System.Web.HttpContext.Current;
string sessionID = context.Session["userID"].ToString();
//Create request and add headers
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Custom header
client.DefaultRequestHeaders.Add("loggedInUser", sessionID);
//Response
HttpResponseMessage response = await client.GetAsync(operation);
if (response.IsSuccessStatusCode)
{
string jsondata = await response.Content.ReadAsStringAsync();
return Content(jsondata, "application/json");
}
return Json(1, JsonRequestBehavior.AllowGet);
}
}
我的Api Broker获取请求并执行此操作:
如您所见,响应内容包含大量反斜杠。
这个回复会回到我的前端,我会收到以下内容:
在此响应中,添加了更多反斜杠。
我希望有人认识到这个问题并知道解决方案。
提前致谢!
我通过将字符串序列化为JSON对象而不是反序列化来修复它。