我打算从 ASP.NET Core 控制器做一个 JSON 响应,但它并不像我想象的那么简单。
一个简单的字符串 {"ConfigValue":"192.168.1.125:1880"}应该被序列化为一个 JsonObject 并且这个对象应该是 JSON 响应的一部分。
但是返回 Json(str) 响应类似于
{\u0022ConfigValue\u0022:\u0022192.168.1.125:1880\u0022}
Run Code Online (Sandbox Code Playgroud)
我不知道如何摆脱 \u0022
public class commonController : Microsoft.AspNetCore.Mvc.Controller
{
private IConfiguration config;
public commonController(IConfiguration configuration)
{
config = configuration;
}
// GET
[Route("getConfigEntry/{key?}")]
public JsonResult getConfigEntry(string? key)
{
Dictionary<string, string> dict = new Dictionary<string, string>
{
{"ConfigValue", "192.168.1.125:1880"}
};
str = JsonConvert.SerializeObject(dict,Formatting.None); //from debugger variable viewer {"ConfigValue":"192.168.1.125:1880"}
return Json(str); // "{\u0022ConfigValue\u0022:\u0022192.168.1.125:1880\u0022}"
}
}
Run Code Online (Sandbox Code Playgroud)