2016-11-08 70 views
2

嘗試創建json時,我不斷收到此錯誤。這是完整的錯誤使用JSON JavaScriptSerializer進行序列化或反序列化時出錯。

Error during serialization or deserialization using the JSON 
JavaScriptSerializer. The length of the string exceeds 
the value set on the maxJsonLength property. 

我的代碼如下

public ActionResult ChangeRequests_Read([DataSourceRequest] DataSourceRequest request) 
    { 
     var data = GetRequests(); 
     var serializer = new JavaScriptSerializer(); 
     var result = new ContentResult(); 
     serializer.MaxJsonLength = Int32.MaxValue; // Whatever max length you want here 
     result.Content = serializer.Serialize(data.ToDataSourceResult(request)); 
     result.ContentType = "application/json"; 
     return result; 

     //return Json(GetRequests().ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
    } 

,從這裏拉這

public static IEnumerable<ChangeRequestsVM> GetRequests() 
    { 
     var model = CompanyContextFactory.GetContextPerRequest(); 
     var chrwVM = model.ChangeRequestsHDRs.Where(ch=> ch.CompanyID == GlobalVariables.CompanyID).Select(ch=> new ChangeRequestsVM 
     { 
      RequestID = ch.RequestID, 
      CompanyID = ch.CompanyID, 
      ClientID = ch.ClientID, 
      EmployeeID = (string.IsNullOrEmpty(ch.EmployeeID)) ? "NA" : ch.EmployeeID, 
      AssignmentType = ch.AssignmentType, 
      Key1 = ch.Key1, 
      Key2 = ch.Key2, 
      LogonID = ch.LogonID, 
      ProcessDate = ch.ProcessDate, 
      ProcessTime = ch.ProcessTime, 
      ProcessUserID = ch.ProcessUserID, 
      RequestDate = ch.RequestDate, 
      RequestExport = ch.RequestExport, 
      RequestNote = ch.RequestNote, 
      RequestOrigin = Convert.ToChar(ch.RequestOrigin), 
      RequestProcess = ch.RequestProcess, 
      RequestStatus = ch.RequestStatus, 
      RequestTime = ch.RequestTime, 
      RequestType = Convert.ToChar(ch.RequestType), 
      ResponseNote = ch.ResponseNote, 
      TableName = ch.TableName, 
      TransferDate = ch.TransferDate, 
      TransferTime = ch.TransferTime, 
      TransferUserID = ch.TransferUserID, 
      dispOrigin = (Convert.ToChar(ch.RequestOrigin) == ChangeRequestOrigin.System) ? "System" : (Convert.ToChar(ch.RequestOrigin) == ChangeRequestOrigin.Client) ? "Client" : "Employee", 
      dispRequestType = (Convert.ToChar(ch.RequestType) == ChangeRequestType.Insert) ? "Insert" : (Convert.ToChar(ch.RequestType) == ChangeRequestType.Alter) ? "Change" : "Delete", 
      dispStatus = FieldTranslation.GetEnumDescription(typeof(enChangeRequestStatus), ch.RequestStatus ?? 0) 
     }).OrderByDescending(ch=> ch.RequestID); 
     return chrwVM; 
    } 

爲什麼我的JSON超過長度是多少?我已經做到了,所以該方法只從數據庫中提取一個項目,但仍然出現錯誤。

+0

也許你的領域之一,有太多的數據,比如'ch.RequestNote'或'ch.ResponseNote'。 –

+0

如果使用'JavaScriptSerializer'對你來說不是一個硬性要求,那麼我建議你切換到Json.NET。 –

回答

1

你需要增加MaxJsonLength屬性的大小:

https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx

這是在配置文件中完成的。詳情請參閱上面的鏈接。

更新:

由於您使用MVC,您可以使用下面的應用程序設置:

<appSettings> 
    <add key="aspnet:MaxJsonDeserializerMembers" value="123456789" /> 
... 
</appSettings> 

但是,要知道,這是一個潛在的安全風險。在這裏閱讀更多:

https://msdn.microsoft.com/en-us/library/hh975440(v=vs.120).aspx

+0

這已經完成 – TheDizzle

相關問題