Json Encode抛出异常json长度超过 [英] Json Encode throwing exception json length exceeded

查看:186
本文介绍了Json Encode抛出异常json长度超过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON JavaScriptSerializer从我的MVC控制器发送json,其抛出异常,序列化或反序列化期间发生错误.字符串的长度超过了在maxJsonLength属性上设置的值.

I am trying to send json from my MVC controller, its throwing exception, Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

我用google搜索并在配置中添加了最大长度,也覆盖了我的json方法,没有任何解决方法.

I googled and added the max length in my config, also overridden my json method, nothing working out.

这是我的网络配置 和我的方法,它的抛出异常. 在设置中

Here is my web config and my method, its throwing exception. in appsetting

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647">
        </jsonSerialization>

    </scripting>
  </system.web.extensions>

乘骑方法

 protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonResult()
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                MaxJsonLength = Int32.MaxValue
            };
        }

我的方法

 public JsonResult GetAttributeControls()
        {
            List<SelectListItem> attrControls;

            using (var context = new Context())
            {
                attrControls = context.AttributeControls.ToList().
                    Select(ac => new SelectListItem { Text = ac.Name, Value = ac.AttributeControlId.ToString() }).ToList();
            }
            //var jsonResult = Json(attrControls, JsonRequestBehavior.AllowGet);
            //jsonResult.MaxJsonLength = int.MaxValue;
            //return jsonResult;
            return Json(attrControls,JsonRequestBehavior.AllowGet);
        }

我在下面的行中遇到异常 这是我的load.chtml文件

I am getting exception in the below line this is my load.chtml file

<script type="text/javascript">
    var InitialRowData = '@Html.Raw(Json.Encode(Model))';
    var isLayoutEditable = false;
    var occupied = '@Model.occupied';
    var notoccupied = '@Model.notoccupied';
    var blocked = '@Model.blocked';
</script>

@ Html.Raw(Json.Encode(Model))';

@Html.Raw(Json.Encode(Model))';

json的最大长度为200000左右,如何增加大小,没有任何效果.有什么帮助吗?

there json maximum length is around 200000, how to increase the size, nothing working out. any help please?

谢谢.

推荐答案

好的,所以我最近遇到了完全相同的问题.我试图做一个@Html.Raw(Json.Encode(Model))将模型发送到javascript,并收到一个错误消息,即字符串太长.

Okay, so I just recently had the exact same issue. I was trying to do an @Html.Raw(Json.Encode(Model)) to send the model through to javascript and was getting an error that the string was too long.

我到处寻找答案,但是找不到直接回答该问题的任何内容,但是,我找到了此堆栈答案然后我用来弄清楚如何解决我们的问题.

I scoured around for an answer to this, and could not find anything that answered the question directly, however, I found This Stack Answer that I then used to figure out how to solve our issue.

这里的想法是设置JavaScriptSerializer,手动设置MaxJsonLength,序列化模型,然后然后将其传递给javascript.

The idea here is to set up a JavaScriptSerializer, manually set the MaxJsonLength, serialize the model, and then pass it through to javascript.

在您的视图的HTML中:

@{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var jsonModel = serializer.Serialize(Model);
}

在您的JavaScript中:

<script>
    $(function () {
        var viewModel = @Html.Raw(jsonModel);
        // now you can access Model in JSON form from javascript
    });
</script>

这篇关于Json Encode抛出异常json长度超过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆