Json.Net And ActionResult

冷暖自知 提交于 2019-12-28 12:14:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!