json.net将字符串反序列化为嵌套类 [英] json.net deserialize string to nested class

查看:135
本文介绍了json.net将字符串反序列化为嵌套类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个类似以下内容的http请求中收到一个Json字符串:

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'.

有人可以看到我在做什么吗?我以为'errors'json键搞砸了,但是我也尝试了一个字符串数组.

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.

推荐答案

我的测试代码无法与嵌套的Info类一起编译(由于属性命名冲突),因此我从ResposeEntity类中将其删除.

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.

与此同时,我修复了JSON的一些问题(您的信息对象是一个数组,并且您的errors数组中的字符串需要用引号引起来).

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).

请参见下文

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;

希望这会有所帮助.

这篇关于json.net将字符串反序列化为嵌套类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆