C# 使用Json.NET的问题

C# 使用Json.NET的问题,c#,json.net,C#,Json.net,我试图使用JSON.NET解析来自API的响应 { "ok": true, "channels": [ { "id": "xxxxx", "name": "xx", "created": "xxxxx", "creator": "xxxxxx", "is_archived": false, "is_member": fal

我试图使用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的输出。因为代币和身份证,我把所有东西都匿名了

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()));
 }
JObject root=JObject.Parse(channelJSON);
foreach(根[“通道”].Children()中的JProperty属性)
{
JObject通道=(JObject)属性值;
添加(新SlackChannel(Channel[“name”].ToString(),Channel[“id”].ToString());
}

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

要将json与json.Net反序列化,可以使用以下方法:

使用Json生成一个类,并:

公共类主题
{
公共字符串值{get;set;}
公共字符串创建者{get;set;}
公共字符串last_set{get;set;}
}
公共阶级目的
{
公共字符串值{get;set;}
公共字符串创建者{get;set;}
公共字符串last_set{get;set;}
}
公共类频道
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
已创建公共字符串{get;set;}
公共字符串创建者{get;set;}
公共布尔值已存档{get;set;}
公共bool是_成员{get;set;}
公共int num_成员{get;set;}
公共布尔是_-general{get;set;}
公共主题主题{get;set;}
公共目的{get;set;}
}
公共类根对象
{
公共bool ok{get;set;}
公共列表频道{get;set;}
}
并从以下位置使用此行:

RootObject m=JsonConvert.DeserializeObject(json);

瞧。

没有使用
json.net
,但是
channels
属性是一个数组,因此循环的标准应该迭代数组,然后对于数组的每个索引,在
中执行
谢谢您的JSON2Charp链接。当然是书签了。事实上,没有json2csharp还有另一种方法可以得到这个。关键是Visual Studio的插件。@JoenJ.Olsen他是VS2012+的本地人,该插件是Microsoft Web Developer Tools。我想知道我以前怎么没有注意到这一点。谢谢你再次找到它!现在我甚至可以为XML做这件事,这非常棒,因为我几乎每天都在处理XML。@user3500033你确定你的json是有效的吗?您确定您的字符串包含整个json吗?顺便说一下,您应该阅读生成的所有代码。
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; }
}
RootObject m = JsonConvert.DeserializeObject<RootObject>(json);