嘗試創建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超過長度是多少?我已經做到了,所以該方法只從數據庫中提取一個項目,但仍然出現錯誤。
也許你的領域之一,有太多的數據,比如'ch.RequestNote'或'ch.ResponseNote'。 –
如果使用'JavaScriptSerializer'對你來說不是一個硬性要求,那麼我建議你切換到Json.NET。 –