I have a Web API controller and from there I'm returning an object as JSON from an action.
I'm doing that like this:
public ActionResult GetAllNotificationSettings()
{
var result = new List<ListItems>();
// Filling the list with data here...
// Then I return the list
return new JsonResult { Data = result };
}
But this way the JsonResult object including its Data
attribute is serialized as JSON. So my final JSON that is return by the action looks like this:
{
"ContentEncoding": null,
"ContentType": null,
"Data": {
"ListItems": [
{
"ListId": 2,
"Name": "John Doe"
},
{
"ListId": 3,
"Name": "Jane Doe"
},
]
},
"JsonRequestBehavior": 1,
"MaxJsonLength": null,
"RecursionLimit": null
}
I can't serialize this JSON string because the JsonResult
object added all kinds of other properties to it. I'm only interested in ListItems
, nothing else. But it automatically added things like: ContentType
, MaxJsonLength
etc...
Now this won't work for me because of all the other properties in the JSON string...
var myList = JsonConvert.DeserializeObject<List<ListItems>>(jsonString);
Is there a way to send a JSON object from the action so that it won't add all the properties that I dont need?
This question is related to
c#
asp.net-mvc
asp.net-mvc-4
asp.net-web-api
json.net
I had a similar problem (differences being I wanted to return an object that was already converted to a json string and my controller get returns a IHttpActionResult)
Here is how I solved it. First I declared a utility class
public class RawJsonActionResult : IHttpActionResult
{
private readonly string _jsonString;
public RawJsonActionResult(string jsonString)
{
_jsonString = jsonString;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var content = new StringContent(_jsonString);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = content };
return Task.FromResult(response);
}
}
This class can then be used in your controller. Here is a simple example
public IHttpActionResult Get()
{
var jsonString = "{\"id\":1,\"name\":\"a small object\" }";
return new RawJsonActionResult(jsonString);
}
When using WebAPI, you should just return the Object rather than specifically returning Json, as the API will either return JSON or XML depending on the request.
I am not sure why your WebAPI is returning an ActionResult, but I would change the code to something like;
public IEnumerable<ListItems> GetAllNotificationSettings()
{
var result = new List<ListItems>();
// Filling the list with data here...
// Then I return the list
return result;
}
This will result in JSON if you are calling it from some AJAX code.
P.S
WebAPI is supposed to be RESTful, so your Controller should be called ListItemController
and your Method should just be called Get
. But that is for another day.
return JsonConvert.SerializeObject(images.ToList(), Formatting.None, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.None, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
using Newtonsoft.Json;
Source: Stackoverflow.com