0

I am trying to deserialize a json string into a list of objects ad keep getting "JsonSerializationException: 'Error converting value to type 'System.Collections.Generic.List" error. I googled for this error but it looks like what I am doing is right.

Does anyone see what is wrong here?

[
    {"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; }
}

This is in response to a button click:

$(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;
});

In code-behind (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);
    }

If I change

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

to

__doPostBack('btnETE', result);

I get: "Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1" error. I guess this means I am attempting to deserialize a single object into a list of objects. But my json clearly shows list of objects.

if I change

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

to

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

I get an array of objects. I can access AreaID using JSON.parse(result)[0].AreaID. In code behind, however, I get an error at:

deserializedArgsList = JsonConvert.DeserializeObject>(parameter);

JsonReaderException: 'Unexpected character encountered while parsing value: o. Path '', line 1, position 1.' It shows "parameter" as [object Object],[object Object], ...,[object,Object].

NoBullMan
  • 2,032
  • 5
  • 40
  • 93

1 Answers1

0

I think you should try implementing the Serializable interface, and generating the 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; }
    }