使用Json.NET的问题

时间:2014-04-04 23:52:55

标签: c# json.net

我正在尝试使用JSON.NET来解析来自API的响应。

{
    "ok": true,
    "channels": [
        {
            "id": "xxxxx",
            "name": "xx",
            "created": "xxxxx",
            "creator": "xxxxxx",
            "is_archived": false,
            "is_member": false,
            "num_members": 2,
            "is_general": false,
            "topic": {
                "value": "",
                "creator": "",
                "last_set": "0"
            },
            "purpose": {
                "value": "",
                "creator": "",
                "last_set": "0"
            }
        },
        {
            "id": "xxxxx",
            "name": "xxxxx",
            "created": "xxxxxx",
            "creator": "xxxxxxx",
            "is_archived": false,
            "is_member": true,
            "num_members": 3,
            "is_general": true,
            "topic": {
                "value": "",
                "creator": "",
                "last_set": "0"
            },
            "purpose": {
                "value": "xxxxx",
                "creator": "",
                "last_set": "0"
            }
        },
        {
            "id": "xxxx",
            "name": "xxxxxxx",
            "created": "xxxxxx",
            "creator": "xxxxxx",
            "is_archived": false,
            "is_member": false,
            "num_members": 2,
            "is_general": false,
            "topic": {
                "value": "",
                "creator": "",
                "last_set": "0"
            },
            "purpose": {
                "value": "xxxxxxxxx",
                "creator": "",
                "last_set": "0"
            }
        },
        {
            "id": "xxxx",
            "name": "xxxxx",
            "created": "xxxxxx",
            "creator": "xxxxxx",
            "is_archived": false,
            "is_member": true,
            "num_members": 2,
            "is_general": false,
            "topic": {
                "value": "",
                "creator": "",
                "last_set": "0"
            },
            "purpose": {
                "value": "xxxxxx",
                "creator": "xxxxx",
                "last_set": "xxxx"
            }
        }
    ]
}

这是api的输出。由于令牌和ID,我对所有内容进行了匿名处理。

JObject root = JObject.Parse(channelJSON);
foreach (JProperty prop in root["channels"].Children<JProperty>())
{
    JObject Channel = (JObject)prop.Value;
    ChannelList.Add(new SlackChannel(Channel["name"].ToString(), Channel["id"].ToString()));
 }

这是我正在使用的代码。 foreach循环永远不会完成,我在循环中放置断点,但只执行foreach行,然后代码停止。我究竟做错了什么。我想迭代json响应,获取每个通道的名称和ID。我从另一个问题中获得了C#代码并修改了它,但我没有得到任何代码执行。

1 个答案:

答案 0 :(得分:2)

要使用Json.Net反序列化json,可以使用:

使用您的Json和json2csharp生成一个类:

public class Topic
{
    public string value { get; set; }
    public string creator { get; set; }
    public string last_set { get; set; }
}

public class Purpose
{
    public string value { get; set; }
    public string creator { get; set; }
    public string last_set { get; set; }
}

public class Channel
{
    public string id { get; set; }
    public string name { get; set; }
    public string created { get; set; }
    public string creator { get; set; }
    public bool is_archived { get; set; }
    public bool is_member { get; set; }
    public int num_members { get; set; }
    public bool is_general { get; set; }
    public Topic topic { get; set; }
    public Purpose purpose { get; set; }
}

public class RootObject
{
    public bool ok { get; set; }
    public List<Channel> channels { get; set; }
}

并使用doc

中的这一行
RootObject m = JsonConvert.DeserializeObject<RootObject>(json);

瞧。