1

I am trying to Use JSON.NET to parse response from an 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. I anonymized everything because of tokens and IDs.

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. What am I doing wrong. I want to iterate through the json response, getting the name and ID for each channel. I got the C# code from another question, and modified it, but I'm not getting any execution of the code.

1
  • Haven't used json.net, but the channels property is an array, so a standard for loop should iterate the array, then for each index of the array, do a for in Commented Apr 4, 2014 at 23:57

1 Answer 1

2

To deserialize a json with Json.Net, you can use this :

Generate a class with your Json and 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 :

RootObject m = JsonConvert.DeserializeObject<RootObject>(json);

voila.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the json2csharp link. Bookmarked for sure.
In fact there is another method to get this without json2csharp. The key is an addon for Visual Studio.
@JoenJ.Olsen He is native in VS2012+ and the addon is Microsoft Web Developer Tools. pic
I wonder how I haven't noticed that before now. Thank you for finding it again! Now I'm even able to do it for XML as well, which is quite awesome since I work with XML pretty much every day.
@user3500033 are you sure your json is valid ? are your sure that your string contain the whole json ? By the way, you should read all the code your generate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.