ON序列化的JavaScriptSerializer或反序列化过

ON序列化的JavaScriptSerializer或反序列化过

本文介绍了如何使用JSON序列化的JavaScriptSerializer或反序列化过程中赶上错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检索JSON,但由于其有限的长度(2147483644),我收到此错误。

我如何能赶上这个错误?

This is how I am coding right now.

[HttpGet]
        public JsonResult GetSearchData(string filter)
        {
            IRemediationService svc = new RemediationService();
            var data = svc.SearchData(filter);
            try{
              return Json(data, JsonRequestBehavior.AllowGet);
            }catch(Exception e){
              return "Error";
            }

    }
解决方案

In MVC 4 you can do:

protected override JsonResult GetSearchData(string filter)
{
    IRemediationService svc = new RemediationService();
    var data = svc.SearchData(filter);
    try
    {
        return new JsonResult()
            {
                Data = data,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                MaxJsonLength = Int32.MaxValue
            };
    }
    catch (Exception e)
    {
        return "Error";
    }
}

You could check data and trim your object down.

If not maybe you can just pass it back as a string...

protected override ActionResult GetSearchData(string filter)
{
    IRemediationService svc = new RemediationService();
    var data = svc.SearchData(filter);
    try
    {
        return Content(data.ToString());
    }
    catch (Exception e)
    {
        return "Error";
    }
}

这篇关于如何使用JSON序列化的JavaScriptSerializer或反序列化过程中赶上错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:23