1

my json is like

{
 "code":"1",
 "message":"The request succeeded",
"contacts":    
           { "contactId":"58330efb45cedb9087e281e6",
             "email":"",
             "firstName":"",
             "lastName":"",
             "number":"4145075733"
           }
}

and i am trying to use DeserializeObject with this Generic.List

public class AllContacts
{
    public string code { get; set; }
    public string message { get; set; }
    public List<ContactList> contacts { get; set; }
}
public class ContactList
{
    public string contactId { get; set; }
    public string email { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string number { get; set; }
}

this is working fine with two or more data in contact array but it's not working in single data.

Error:

Cannot deserialize the current JSON object (eg.{"name":"value"}) into type 'System.Collections.Generic.List'1[smsApplication.Controller‌​s.ContactList]' because the type requires a JSON array to deserialize correctly. To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

3
  • please define "not working". You get an error message, or it doesn't produce the expected result? Give an example. Commented Dec 2, 2016 at 14:02
  • "Not working" gives error Cannot deserialize the current JSON object (eg.{"name":"value"}) into type 'System.Collections.Generic.List`1[smsApplication.Controllers.ContactList]' because the type requires a JSON array to deserialize correctly. To fix this error either change the JSON to a JSON array or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Commented Dec 2, 2016 at 14:05
  • Another approach, you could use JObject and SelectToken to pull in the data. Commented Dec 2, 2016 at 15:16

1 Answer 1

1

Thats because contact is a json object and not a json array

Either update the json to match the defined classes

{
 "code":"1",
 "message":"The request succeeded",
"contacts":[    
           { "contactId":"58330efb45cedb9087e281e6",
             "email":"",
             "firstName":"",
             "lastName":"",
             "number":"4145075733"
           }]
}

Or update the classes to match the json

public class AllContacts
{
    public string code { get; set; }
    public string message { get; set; }
    public ContactList contacts { get; set; }
}

public class ContactList
{
    public string contactId { get; set; }
    public string email { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string number { get; set; }
}

The error message explains what you do.

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.