[英]Issues using Json.NET
I am trying to Use JSON.NET to parse response from an API. 我正在尝试使用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"
}
}
]
}
this is the output of the api. 这是api的输出。 I anonymized everything because of tokens and IDs.
由于令牌和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()));
}
This is the code I am using. 这是我正在使用的代码。 The foreach loop never completes, I placed breakpoints in the loop, but only the foreach line executes, then the code stops.
foreach循环永远不会完成,我在循环中放置了断点,但是仅执行了foreach行,然后代码停止了。 What am I doing wrong.
我究竟做错了什么。 I want to iterate through the json response, getting the name and ID for each channel.
我想遍历json响应,获取每个通道的名称和ID。 I got the C# code from another question, and modified it, but I'm not getting any execution of the code.
我从另一个问题中获得了C#代码,并对其进行了修改,但是我没有得到任何代码执行。
To deserialize a json with Json.Net, you can use this : 要使用Json.Net反序列化json,可以使用以下命令:
Generate a class with your Json and json2csharp : 用您的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; }
}
And use this line from the doc : 并使用doc中的这一行:
RootObject m = JsonConvert.DeserializeObject<RootObject>(json);
voila. 瞧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.