无法反序列化当前的JSON数组

时间:2014-11-28 16:51:48

标签: c# json serialization xamarin json.net

我正在编写一个使用Newtonsoft.Json的Xamarin应用程序,但我遇到以下错误消息时出现问题:

  

“无法将当前JSON数组(例如[1,2,3])反序列化为类型   'learningAndroidICS.memberInfo'因为类型需要JSON   反对正确反序列化的对象。要修复此错误,请更改   JSON到JSON目标或将反序列化类型更改为数组或   一个实现集合的类型,如List,可以是   从JSON数组序列化。“

考虑到我的代码如下:

currentMember = JsonConvert.DeserializeObject<memberInfo> (content);

其中currentMember是memberInfo类的一个实例,如下所示:

using System;

namespace learningAndroidICS
{
    public class memberInfo
    {
        public string id { get; set; }
        public string lastName { get; set; }
        public string firstName1 { get; set; }
        public string firstName2 { get; set; }
        public string address1 { get; set; }
        public string address2 { get; set; }
        public string childName1 { get; set; }
        public string childName2 { get; set; }
        public string childName3 { get; set; }
        public string childName4 { get; set; }
        public string childName5 { get; set; }
        public string childName6 { get; set; }
        public string childName7 { get; set; }
        public string childName8 { get; set; }
        public string childName9 { get; set; }
        public string childName10 { get; set; }
        public string childDOB1 { get; set; }
        public string childDOB2 { get; set; }
        public string childDOB3 { get; set; }
        public string childDOB4 { get; set; }
        public string childDOB5 { get; set; }
        public string childDOB6 { get; set; }
        public string childDOB7 { get; set; }
        public string childDOB8 { get; set; }
        public object childDOB9 { get; set; }
        public string childDOB10 { get; set; }
        public string dateActivated { get; set; }
        public string dateDeactivated { get; set; }
        public string isActive { get; set; }
        public int membershipType { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string email { get; set; }

        public memberInfo ()
        {
        }
    }
}

JSON字符串(来自Web服务)如下所示:

[
    {
        "id": "8",
        "lastName": "Smith",
        "firstName1": "John",
        "firstName2": "Jane",
        "address1": "123 Fake Ave",
        "address2": "",
        "childName1": "Bill",
        "childName2": "John",
        "childName3": "Jim",
        "childName4": "",
        "childName5": "",
        "childName6": "",
        "childName7": null,
        "childName8": null,
        "childName9": null,
        "childName10": null,
        "childDOB1": "11/02/1991",
        "childDOB2": "22/10/1992",
        "childDOB3": "11/08/1998",
        "childDOB4": "",
        "childDOB5": "",
        "childDOB6": "",
        "childDOB7": null,
        "childDOB8": null,
        "childDOB9": null,
        "childDOB10": null,
        "dateActivated": "26/11/2014",
        "dateDeactivated": "",
        "isActive": "1",
        "membershipType": null,
        "city": "Fake",
        "state": "MI",
        "zip": "12345",
        "email": "fake@gmail.com"
    }
]

1 个答案:

答案 0 :(得分:4)

这里有两个问题:

  1. 您的JSON表示对象列表,但您尝试反序列化为不是列表的类。这就是你得到例外的原因。要解决此问题,您需要更改代码:

    var list = JsonConvert.DeserializeObject<List<memberInfo>>(content);
    currentMember = list[0];
    

    注意上面的代码假设列表总是至少有一个元素。如果列表可以为空或为null,则必须调整代码才能处理。

  2. 在您的JSON中,membershipType属性的值为null,但在您的类中,它被声明为int。反序列化时,这会导致错误,因为无法将null分配给int.要解决此问题,您需要将membershipType属性更改为int?

    public int? membershipType { get; set; }
    
  3. 一旦解决了这两个问题,反序列化就可以正常工作。