I am trying to query the Reddit API and looping over answers for a thread to get the answer I want. However, when I query the api (https://www.reddit.com/comments/2pfyg8.json?sort=top as an example) I'll get an json array with two objects. I want to loop over object number 2 because this is the object that has all the actual comments and the first object is the thread itself.
This seems like a challange in C#, or is atleast a challange for me. I use JSON.NET or Newtonsoft.Json to accomplish this and this is what I have so far:
var commentPath = $"http://www.reddit.com/comments/{questionId}.json?sort=top";
HttpResponseMessage commentResponse = await client.GetAsync(commentPath);
var commentJson = await response.Content.ReadAsStringAsync();
var answers = JsonConvert.DeserializeObject<dynamic>(commentJson);
int commentCount = 0;
foreach (var answerContainer in answers[1].data.children) { }
I have also tried to use http://json2csharp.com/ to generate the correct type for me to use instead of dynamic but it seems to be incorrect as well.
This is the error I recieve:
System.ArgumentException: Accessed JObject values with invalid key value: 1. Object property name expected. vid Newtonsoft.Json.Linq.JObject.get_Item(Object key) vid CallSite.Target(Closure , CallSite , Object , Int32 )
I would be very glad if someone were able to help me in my quest to get the comment I am looking for.