json.net将字符串反序列化为嵌套类

时间:2011-08-24 20:39:38

标签: c# json json.net

我从一个看起来像这样的http请求中收到一个Json字符串:

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

我正在尝试反序列化的实体是基于相同的

构建的
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; }
        }
    }
}

当我尝试反序列化时,虽然我得到了异常

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

有人能看出我做错了什么吗?我在想'错误'json键是搞乱了,但我也尝试了一个字符串数组。

1 个答案:

答案 0 :(得分:5)

我的测试代码无法使用嵌套的Info类进行编译(由于属性命名冲突),因此我将其从ResposeEntity类中删除。

除此之外,我修复了你的JSON的一些问题(你的信息对象是一个数组,你的错误数组中的字符串需要在引号中)。

见下文:

<强> JSON

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

<强>类

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; }
    }
}

测试代码

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;

希望这有帮助。