Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I received a JSON file, which contains a root element "users", and a list of "user" items.

I'm trying to deserialize the json into a List of a custom class called User, but I keep getting a JsonSerializationException, that it can't cover it.

I tried the following:

Code:

 public class User
{
    public int ID { get; set; }
    public bool Active { get; set; }
    public string Name { get; set; }
}

public class Response
{
    public List<User> Users { get; set; }
    public JObject Exception { get; set; }
}

And -

public Response DeserializeJSON(string json)
    {
        Response deserialized = JsonConvert.DeserializeObject<Response>(json);
        return deserialized;
    }

JSON:

    {
  "Users": {
        "User": [
          {
            "id": "1",
            "active": "true",
            "name": "Avi"
          },
          {
            "id": "2",
            "active": "false",
            "name": "Shira"
          },
          {
            "id": "3",
            "active": "false",
            "name": "Moshe"
          },
          {
            "id": "4",
            "active": "false",
            "name": "Kobi"
          },
          {
            "id": "5",
            "active": "true",
            "name": "Yael"
          }
        ]
      }
}

Sorry for bad styling!!

share|improve this question
    
Did you make sure that JSON was valid? Try dumping it here jsonlint.com and see it there ain't some problems in JSON itself. –  tereško Jun 17 '12 at 14:12
    
It was the first thing I did :) It's indeed valid. –  user1461793 Jun 17 '12 at 14:26

2 Answers 2

In your Response class, try initializing the collection in the constructor.

public class Response
{
    public Response()
    {
        Users = new List<User>();
    }
    public IEnumerable<User> Users { get; set; }
    public JObject Exception { get; set; }
}
share|improve this answer
    
Nope, didn't help... same error. what could it be??? –  user1461793 Jun 17 '12 at 13:58
    
Next thing to try is keep code above but change the collection type to IEnumerable (keep as List in constructor). I have edited the solution above –  BlackSpy Jun 17 '12 at 13:59
    
Still doesn't work. On debugging, it does get to the Response's constructor, but the List<User> remains empty. –  user1461793 Jun 17 '12 at 14:19
    
Could the problem be with the JSON- I now notice that it's actually a "Users" object, holding a "User" object, which holds an array of objects. maybe that's the problem? –  user1461793 Jun 17 '12 at 14:22
    
Yes - The both capitalisation of the field names in the User object is wrong (should be uppercase ID, Active and Name) and the User should be deleted - "Users" is the collection –  BlackSpy Jun 17 '12 at 14:23

Ahhh I need start reading the JSON better... :) My problem was that there were infact 2 "wrappers" to this JSON string:

The root element was "Users", which held an element called "User". This fixed it:

public class User
{
    public int id { get; set; }
    public bool active { get; set; }
    public string name { get; set; }
}

public class Response
{
    public ResponseContent users { get; set; }
}

public class ResponseContent
{
    public List<User> user;
}

Thanks! :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.