Reputation: 12007
I have an MVC
ApiController
class in which I am trying to return JSON.
I am using Newtonsoft's JSON.NET
serializer in the method:
// 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).
Upvotes: 0
Views: 494
Reputation: 3204
There is a simple method without using 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
{"Package 1":"One","Package 2":"Two","Package 3":"Three","Package 4":"Four"}
Upvotes: 1