简体   繁体   English

无法反序列化当前JSON数组

[英]Could not deserialize the current JSON array

I'm writing a Xamarin app that uses Newtonsoft.Json and I'm having trouble with the following error message: 我正在编写使用Newtonsoft.Json的Xamarin应用程序,并且遇到以下错误消息:

"Cannot deserialize the current JSON array (eg [1,2,3]) into type 'learningAndroidICS.memberInfo' because the type requires a JSON object to deserialize correctly. To fix this error either change the JSON to a JSON objecct or change the deserialized type to an array or a type that implements a collection interace like List that can be serialized from a JSON array." “无法将当前JSON数组(例如[1,2,3])反序列化为类型'learningAndroidICS.memberInfo',因为该类型需要JSON对象才能正确反序列化。要解决此错误,请将JSON更改为JSON对象或更改将其反序列化为数组,或实现可实现从JSON数组序列化的List之类的集合接口的类型。”

Considering that my code looks like this: 考虑到我的代码看起来像这样:

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

where currentMember is an instance of the memberInfo class, which looks like this: 其中,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 ()
        {
        }
    }
}

The JSON string (comes from a webservice) looks like this: 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"
    }
]

There are two issues here: 这里有两个问题:

  1. Your JSON represents a list of objects, but you are trying to deserialize into a class that is not a list. 您的JSON表示对象列表,但是您尝试反序列化为非列表类。 That is why you are getting an exception. 这就是为什么您要例外。 To fix this, you need to change your code: 要解决此问题,您需要更改代码:

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

    Note the above code assumes the list will always have at least one element. 请注意,以上代码假定列表将始终至少包含一个元素。 If it is possible the list can be empty or null, you will have to adjust your code to handle that. 如果列表可以为空或为空,则必须调整代码以处理该列表。

  2. In your JSON, the membershipType property has the value null , but in your class it is declared as an int . 在您的JSON中, membershipType属性的值为null ,但是在您的类中,该属性被声明为int When you deserialize, this will cause an error because null can't be assigned to an int. 反序列化时,这将导致错误,因为不能将null分配给int. To fix this you will need to change the membershipType property to int? 要解决此问题,您将需要将membershipType属性更改为int? instead. 代替。

     public int? membershipType { get; set; } 

Once you fixed both of those problems the deserialization should work correctly. 解决了这两个问题后,反序列化应该正确运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM