问题
Im building a JObject myself and want to return it as ActionResult. I dont want to create and then serialize a data object
For example
public ActionResult Test(string id)
{
var res = new JObject();
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
res["id"] = 1;
res["result"] = array;
return Json(res); //???????
}
回答1:
You should just be able to do this in your action method:
return Content( res.ToString(), "application/json" );
回答2:
In case, if you take care of JSON Formatting , just return JSON Formatted string
public string Test(string id)
{
var res = new JObject();
JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));
res["id"] = 1;
res["result"] = array;
return YourJSONSerializedString;
}
else Use built in JsonResult(ActionResult)
public JsonResult Test(string id)
{
return Json(objectToConvert);
}
来源:https://stackoverflow.com/questions/21938907/json-net-and-actionresult