3

I'm receiving a Json string back from a http request that looks something like this:

{
  "info":
     [
        {
           "calls":0,
           "errors":"[error1, error2, error3]",
           "messages":0,
           "mail":3
        }
    ],
 "received":5,
 "valid":3
}

The entity I'm trying to deserialize into is structured about the same

class ResponseEntity
{
    private Info info;        
    private int received;
    private int valid;

    [JsonProperty("info")]
    public Info Info
    {
        get { return info; }
        set { info = value; }
    }

    [JsonProperty("valid")]
    public int valid
    {
        get { return valid; }
        set { valid = value; }
    }

    [JsonProperty("received")]
    public int received
    {
        get { return received; }
        set { received = value; }
    }

    public class Info
    {
        private int calls;
        private List<string> errors;
        private int messages;
        private int mail;

        [JsonProperty("calls")]
        public int Calls
        {
            get { return calls; }
            set { calls = value; }
        }

        [JsonProperty("messages")]
        public int Messages
        {
            get { return messages; }
            set { messages = value; }
        }

        [JsonProperty("errors")]
        public List<string> Errors
        {
            get { return errors; }
            set { errors = value; }
        }

        [JsonProperty("mail")]
        public int Mail
        {
            get { return mail; }
            set { mail = value; }
        }
    }
}

When I try to deserialize it though I'm getting an exception

ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;
Cannot deserialize JSON array into type 'CSharpRestService.ResponseEntity+Info'.

Can anybody see what I'm doing wrong? I'm thinking the 'errors' json key is messing things up, but I also tried a string array.

1
  • The property names should have the first letter capitalized. See "valid" & "recieved" Commented Aug 24, 2011 at 20:45

1 Answer 1

8

My test code would not compile with the nested Info class (due to a property naming conflict) so I removed it from within the ResposeEntity class.

Along with this I fixed some issues with your JSON (your info object was an array and the strings in your errors array needed to be in quotes).

see below:

JSON

{
    info":
        {
            "calls":0,
            "errors":["error1", "error2", "error3"],
            "messages":0,
            "mail":3
        },
    "received":5,
    "valid":3
}

Classes

class ResponseEntity
{
    private Info info;
    private int received;
    private int valid;

    [JsonProperty("info")]
    public Info Info
    {
        get { return info; }
        set { info = value; }
    }

    [JsonProperty("valid")]
    public int Valid
    {
        get { return valid; }
        set { valid = value; }
    }

    [JsonProperty("received")]
    public int Received
    {
        get { return received; }
        set { received = value; }
    }
}

public class Info
{
    private int calls;
    private List<string> errors;
    private int messages;
    private int mail;

    [JsonProperty("calls")]
    public int Calls
    {
        get { return calls; }
        set { calls = value; }
    }

    [JsonProperty("messages")]
    public int Messages
    {
        get { return messages; }
        set { messages = value; }
    }

    [JsonProperty("errors")]
    public List<string> Errors
    {
        get { return errors; }
        set { errors = value; }
    }

    [JsonProperty("mail")]
    public int Mail
    {
        get { return mail; }
        set { mail = value; }
    }
}

Test Code

string json = "{\"info\":{\"calls\":0,\"errors\":[\"error1\", \"error2\", \"error3\"],\"messages\":0,\"mail\":3},\"received\":5,\"valid\":3}";

ResponseEntity ent = JsonConvert.DeserializeObject<ResponseEntity>(json) as ResponseEntity;

Hope this helps.

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

1 Comment

Thanks, I wasn't thinking the issue would be with the actual JSON, I wasn't looking closely enough. I've contacted the provider sending me the data and they are going to fix it.

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.