与MVC相比,为什么这种JSON反序列化在MVC3中不起作用

时间:2012-02-09 13:31:55

标签: .net asp.net-mvc asp.net-mvc-3

我有一个MVC(v1)应用程序,它使用以下自定义模型绑定器来反序列化入站JSON数据。

  public class JsonModelBinder : DefaultModelBinder
  {
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      if (!IsJSONRequest(controllerContext))
      {
        return base.BindModel(controllerContext, bindingContext);
      }

      // Get the JSON data that's been posted
      var request = controllerContext.HttpContext.Request;
      var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();

      // Use the built-in serializer to do the work for us
      return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
    }

    private static bool IsJSONRequest(ControllerContext controllerContext)
    {
      var contentType = controllerContext.HttpContext.Request.ContentType;
      return contentType.Contains("application/json");
    }
  }
}

以下DataItemColleciton类和包含的DataItem类在MVC(v1)中正确反序列化。


public class DataItemCollection
{

  #region Constructors

  public DataItemCollection()
  {
    this.dataItems = new List<DataItem>();
  }

  public DataItemCollection(string UserName, string UserInitials, int JobNum, int ObjectVersion, int StationID)
  {
    this.userName = UserName;
    this.userInitials = UserInitials;
    this.jobNum = JobNum;
    this.objectVersion = ObjectVersion;
    this.stationID = StationID;
  }

  public DataItemCollection(string UserName, string UserInitials, int JobNum, int ObjectVersion, int StationID, List<DataItem> DataItems)
  : this(UserName, UserInitials, JobNum, ObjectVersion, StationID)
  {
    this.dataItems = DataItems;
  }

  #endregion

  #region Properties

  public string userName { get; set; }
  public string userInitials { get; set; }
  public int jobNum { get; set; }
  public int objectVersion { get; set; }
  public int stationID { get; set; }
  public List<DataItem> dataItems { get; set; }

  #endregion

}

}


public class DataItem
{

  #region Enums

  public enum DataItemType
  {
    SignOff = 1,
    Material = 2,
    Task = 3,
    ShippingInfo = 4,
    Count = 5
  }

  #endregion

  #region Constructors

  public DataItem() { }

  public DataItem(DataItemType ItemType, int DataLength)
  {
    this.itemType = ItemType;
    //Creates array of specified item type for easier access later if necessary...
    switch (ItemType)
    {
      case DataItemType.SignOff:
        this.itemData = new ViewModels.JobTracking.SignOff[DataLength];
        break;
      case DataItemType.Material:
        this.itemData = new ViewModels.JobTracking.Material[DataLength];
        break;
      case DataItemType.Task:
        this.itemData = new ViewModels.JobTracking.Task[DataLength];
        break;
      case DataItemType.ShippingInfo:
        this.itemData = new ViewModels.JobTracking.ShippingInfo[DataLength];
        break;
      case DataItemType.Count:
        this.itemData = new ViewModels.JobTracking.Count[DataLength];
        break;
    }
  }

  #endregion

  #region Properties

  public DataItemType itemType { get; set; }
  public object[] itemData { get; set; }

  #endregion

  }
}

在MVC3中,反序列化的JSON具有DataItemCollection中的属性,但看起来DataItem对象未正确反序列化。

DataItemCollection
  .userName
  .userInitials
  .jobNum
  .objectVersion
  .stationID
  .dataItems

.dataItems属性包含一系列通用对象,其中没有任何内容。

与原始MVC相比,MVC3反序列化有什么不同会导致这种情况不起作用?另外,关于如何修复它对现有代码的影响最小的任何想法?

1 个答案:

答案 0 :(得分:1)

我不确定在这种情况下MVC 1和MVC 3之间的区别是什么,但我通常使用JSON.NET,它更强大但非常轻量级地序列化和反序列化为json。

http://json.codeplex.com/