JsonSerializationException错误将json字符串转换为对象列表

时间:2018-04-23 19:03:04

标签: c# json jsonexception

我正在尝试将json字符串反序列化为对象列表,并继续获取" JsonSerializationException:'将值转换为类型' System.Collections.Generic.List"错误。我用Google搜索了这个错误,但看起来我正在做的是对的。

有人看到这里有什么问题吗?

[
    {"Name":"ATLANTA PDC","Area":"CAPITAL METRO","District":"ATLANTA","Manager":"y, x","Email":"x.y@here.com","Phone":"(555) 555-3381","StartDate":"2016-07-22T15:23:11","Zips":" 1193,31192,30304,30348,30353,30368,30369,30374,30384,31195","FirstName":"x","LastName":"y","ID":3606,"AreaID":11,"DistrictID":1507},

    {"Name":"MPOO0","Area":"CAPITAL METRO","District":"ATLANTA","Manager":"yy, xx","Email":"xx.yy@here.com","Phone":"(555) 555-7709","StartDate":"2016-07-22T15:21:58","Zips":" 0325,30324,30328,30332,30364,30366,30370,30371,30375,30377,30378,30380,30385,30388,30392,30394,30396,30398,31106,31107,30333,30334,30336,30337,30338,30339,30340,30341,30342,30343,30344,30345,30346,30349,30350,30354,30355,30356,30357,30358,30359,30360,30326,30327,30329,30361,30363,30301,30302,30303,30305,30306,30307,30308,30309,30310,30311,30312,30313,30314,30315,30316,30317,30318,30319,30321,30322,31119,31126,31131,31136,31139,31141,31145,31146,31150,31156,31196,39901,30331,30362","FirstName":"xx","LastName":"yy","ID":3604,"AreaID":11,"DistrictID":1507},
]

public class MPOOData
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Manager { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Area { get; set; }
    public string District { get; set; }
    public int AreaID { get; set; }
    public int DistrictID { get; set; }
    public DateTime StartDate { get; set; }
    public string Zips { get; set; }
}

这是对按钮点击的响应:

$(document).on("click", "#btnExpToExcel", function (event) {debugger
    if (CheckEmptyGrid() == 0) {
        alert('No records to export to Excel');
        return false;
    }
    var userAreaID = '<%= Session["AreaID"] %>';
    var userDistrictID = '<%= Session["DistrictID"] %>';

    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: '<%= ResolveUrl("services/mpoo.asmx/GetMPOOListDS_withZips") %>',
        cache: false,
        data: JSON.stringify({ "AreaID":userAreaID , "DistrictID":userDistrictID  }),
    }).done(function (data) {debugger
        var result = data.d;

        if (result != '') {
            __doPostBack('btnETE', JSON.stringify(result));
        }
    });
    return false;
});

在代码隐藏(C#)中:

string parameter = Request["__EVENTARGUMENT"];
string target = Request["__EVENTTARGET"];

if (!string.IsNullOrEmpty(target) && !string.IsNullOrEmpty(parameter))
{
    List<MPOOData> deserializedArgsList = new List<MPOOData>();

    if (target.Equals("btnETE"))
    {
        //var table = JsonConvert.DeserializeObject<DataTable>(parameter);
        deserializedArgsList = JsonConvert.DeserializeObject<List<MPOOData>>(parameter);
        dtMPOO = deserializedArgsList.ToDataTable<MPOOData>();
        ExportToExcel(dtMPOO);
    }

如果我改变

__doPostBack('btnETE', JSON.stringify(result));

__doPostBack('btnETE', result);

我得到:&#34;无法将当前JSON对象(例如{“name”:“value”})反序列化为类型System.Collections.Generic.List`1&#34;错误。我想这意味着我试图将单个对象反序列化为对象列表。但我的json清楚地显示了对象列表。

如果我改变

__doPostBack('btnETE', JSON.stringify(result));

__doPostBack('btnETE', JSON.parse(result))

我得到一个对象数组。我可以使用JSON.parse(result)[0] .AreaID访问AreaID。但是,在后面的代码中,我收到错误:

deserializedArgsList = JsonConvert.DeserializeObject&gt;(参数);

JsonReaderException:&#39;解析值时遇到意外的字符:o。路径&#39;&#39;,第1行,第1位。&#39;它显示&#34;参数&#34; as [object Object],[object Object],...,[object,Object]。

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试实现Serializable接口,并生成serialVersionUID。

public class MPOOData implements Serializable
    {
        private static final long serialVersionUID = -6572712057013837374L;

        public int ID { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Manager { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Area { get; set; }
        public string District { get; set; }
        public int AreaID { get; set; }
        public int DistrictID { get; set; }
        public DateTime StartDate { get; set; }
        public string Zips { get; set; }
    }