public void DoesJsonDeserialize() { string jsonMessage = File.ReadAllText("HttpSampleResponse.json"); RedditCommentQueryResponse response = JsonConvert.DeserializeObject <RedditCommentQueryResponse>(jsonMessage); Assert.IsNotNull(response); Assert.AreEqual(2, response.data.children.Length, "Two comments should be deserialized"); for (int i = 0; i < 2; ++i) { Assert.AreEqual($"author{i + 1}", response.data.children[i].data.author); } }
public IEnumerable <RedditComment> GetRecentComments(int numComments) { List <RedditComment> output = new List <RedditComment>(); using (var httpResponse = httpClient.GetAsync(GetRecentCommentsRequestUrl(numComments)).Result) { HttpContent content = httpResponse.Content; if (httpResponse.StatusCode != HttpStatusCode.OK || content == null) { return(output); } RedditCommentQueryResponse parsedContent = JsonConvert.DeserializeObject <RedditCommentQueryResponse>(content.ReadAsStringAsync().Result); foreach (Comment redditComment in parsedContent?.data?.children) { RedditComment toAdd = new RedditComment(redditComment.data); output.Add(toAdd); } } return(output); }