0

I'm writing a Xamarin app that uses Newtonsoft.Json and I'm having trouble with the following error message:

"Cannot deserialize the current JSON array (e.g. [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."

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:

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:

[
    {
        "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": "[email protected]"
    }
]

1 Answer 1

4

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. 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. When you deserialize, this will cause an error because null can't be assigned to an int. To fix this you will need to change the membershipType property to int? instead.

    public int? membershipType { get; set; }
    

Once you fixed both of those problems the deserialization should work correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.