如何使用JSON序列化的JavaScriptSerializer或反序列化过程中赶上错误 [英] How to catch error during serialization or deserialization using the JSON JavaScriptSerializer
本文介绍了如何使用JSON序列化的JavaScriptSerializer或反序列化过程中赶上错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检索JSON,但由于其有限的长度(2147483644),我收到此错误。
我如何能赶上这个错误?
使用JSON序列化和反序列化过程中的错误
的JavaScriptSerializer。字符串的长度超过设定的值
在maxJsonLength属性。
块引用>这是怎么了,现在的编码。
[HTTPGET]
公共JsonResult GetSearchData(字符串过滤器)
{
IRemediationService SVC =新RemediationService();
VAR数据= svc.SearchData(过滤器);
尝试{
返回JSON(数据,JsonRequestBehavior.AllowGet);
}赶上(例外五){
返回错误;
} }
解决方案在MVC 4,你可以这样做:
保护覆盖JsonResult GetSearchData(字符串过滤器)
{
IRemediationService SVC =新RemediationService();
VAR数据= svc.SearchData(过滤器);
尝试
{
返回新JsonResult()
{
数据=数据,
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
MaxJsonLength = Int32.MaxValue
};
}
赶上(例外五)
{
返回错误;
}
}您可以检查数据,并修剪你的对象了。
如果没有,也许你可以把它传递回为一个字符串...
保护覆盖的ActionResult GetSearchData(字符串过滤器)
{
IRemediationService SVC =新RemediationService();
VAR数据= svc.SearchData(过滤器);
尝试
{
返回含量(data.ToString());
}
赶上(例外五)
{
返回错误;
}
}I am retrieving JSON but due to its limited length (2147483644), I am getting this error.
How can I catch this error?
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
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或反序列化过程中赶上错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文