{"没有为' System.String []'。"}的类型定义无参数构造函数,同时取消json数据的deserails

时间:2014-05-17 11:53:36

标签: json asp.net-mvc-4 deserialization

这是我的控制器

[NoCache]
    public ActionResult SaveAssociate(string data)
    {
        JavaScriptSerializer json = new JavaScriptSerializer();
        List<string> mystring = json.Deserialize<List<string>>(data);
        return Content("Hai");
    }

表达数据 价值:

[{"AssetName":"8888","AssetNumber":"8888","Classification":null,"ParentAsset":null,"SerialNumber":"8888","ParentCompany":"JPL Holdings","Barcode":null,"RFIDTags":null,"AssetId":"dfe2ae51-f153-4a67-bd3b-0114d8a40751","Notes":null,"Manufacturer":null,"DepartmentId":null,"Department":null,"SupplierId":null,"Supplier":null},{"AssetName":"552014","AssetNumber":"552014","Classification":null,"ParentAsset":"8888","SerialNumber":"552014","ParentCompany":"JPL Holdings","Barcode":null,"RFIDTags":null,"AssetId":"4109ba40-af78-486a-a40e-1d14a2d7b42f","Notes":null,"Manufacturer":null,"DepartmentId":null,"Department":null,"SupplierId":null,"Supplier":null},{"AssetName":"201","AssetNumber":"201","Classification":null,"ParentAsset":null,"SerialNumber":"2011","ParentCompany":"JPL Holdings","Barcode":null,"RFIDTags":null,"AssetId":"3e552280-16df-4c17-a4a5-1f61c4c96835","Notes":null,"Manufacturer":null,"DepartmentId":null,"Department":null,"SupplierId":null,"Supplier":null}]

在这一行List<string> mystring = json.Deserialize<List<string>>(data); 我正在

{"No parameterless constructor defined for type of 'System.String[]'."}

即使我尝试过这样的

 [Serializable]
public class GetUser
{
    public GetUserdata[] Data { get; set; }
}

[Serializable]
public class GetUserdata
{
    public string AssetName { get; set; }
    public string AssetNumber { get; set; }
    public string Classification { get; set; }
    public string ParentAsset { get; set; }
    public string SerialNumber { get; set; }
    public string ParentCompany { get; set; }
    public string Barcode { get; set; }
    public string RFIDTags { get; set; }
    public string AssetId { get; set; }
    public string Notes { get; set; }
    public string Manufacturer { get; set; }
    public string DepartmentId { get; set; }
    public string Department { get; set; }
    public string SupplierId { get; set; }
    public string Supplier { get; set; }
}

控制器

 public ActionResult SaveAssociate(string data)
    {
        JavaScriptSerializer json = new JavaScriptSerializer();
        GetUser mystring = json.Deserialize<GetUser>(data);
        return Content("Hai");
    }

收到错误 输入&#39; AssetTrackingSystem.Model.GetUser&#39;不支持反序列化数组。

帮帮我

1 个答案:

答案 0 :(得分:1)

您可以尝试:

而不是上面的代码
 public ActionResult SaveAssociate(string data)
 {
    JavaScriptSerializer json = new JavaScriptSerializer();
    GetUserdata[] myUsers = json.Deserialize<GetUserdata[]>(data);
    return Content("Hai");
  }

因为您的Json是作为对象数组发送的,而不是数组的对象。 我希望它会对你有所帮助。