I have an MVC
ApiController
class in which I am trying to return JSON.
我有一個MVC ApiController類,我試圖返回JSON。
I am using Newtonsoft's JSON.NET
serializer in the method:
我在方法中使用Newtonsoft的JSON.NET序列化程序:
// GET api/packages/5
public string Get(int id)
{
var Packages = new Dictionary<string, string>();
Packages.Add("Package 1", "One");
Packages.Add("Package 2", "Two");
Packages.Add("Package 3", "Three");
Packages.Add("Package 4", "Four");
return JsonConvert.SerializeObject(Packages);
}
But when viewed in my browser, I see the response:
但是在瀏覽器中查看時,我看到了響應:
"{\"Package 1\":\"One\",\"Package 2\":\"Two\",\"Package 3\":\"Three\",\"Package 4\":\"Four\"}"
with all the quotes escaped. This is obviously not readable by my client. Can anyone see where I am going wrong here? The content-type
is returned as application/json
(which is shown in Chrome developer tools also).
所有引號都逃脫了。這顯然是我的客戶無法讀取的。誰能看到我在哪里錯了? content-type作為application / json返回(也在Chrome開發人員工具中顯示)。
1
There is a simple method without using JSON.NET.
有一種簡單的方法,不使用JSON.NET。
public Dictionary<string, string> Get()
{
var packages = new Dictionary<string, string>
{
{"Package 1", "One"},
{"Package 2", "Two"},
{"Package 3", "Three"},
{"Package 4", "Four"}
};
return packages;
}
The result in IE is
IE的結果是
{"Package 1":"One","Package 2":"Two","Package 3":"Three","Package 4":"Four"}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/09/01/725493f598e51339ff87eb5a1d037136.html。