M
M
miXtik7892021-12-01 16:25:55
ASP.NET
miXtik789, 2021-12-01 16:25:55

Cannot access child value on Newtonsoft.Json.Linq.JProperty c# how to fix?

Please help with a bug:
I wanted to make a parser for the number of subscribers of the VK group, here is the code:

URI3 = "https://api.vk.com/method/groups.getMembers?group_id=208032066&v=5.81&offset=0&access_token=fbaa5375055f5273104712&&&&&&&&&&&&&&&&&&&&&&&";
                                        result3 = webclient3.DownloadString(URI3);
                                        Console.WriteLine(result3);
                                        int countofmembers = 0;
                                        int ofset = 0;
                                        var msgcol = JObject.Parse(result3)["response"].ToList();
                                        foreach (var item in msgcol)//проходимся по коллекции.
                                        {
                                            countofmembers = Convert.ToInt32(item["count"].ToString());
                                            double rond = countofmembers / 1000;
                                            ofset = (int)Math.Round(rond, 0);
                                            Console.WriteLine(ofset);
                                        }

the error happens here:
var msgcol = JObject.Parse(result3)["response"].ToList();

Cannot access child value on Newtonsoft.Json.Linq.JProperty
and here is the json that comes in:
{"response":{"count":10,"items":[84922,728576,6581510,15401780,21896850,95414703,213498474,227627901 ,367465044,642091787]}}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-12-01
@miXtik789

In theory, msgcol should be JArray and not JObject.
Try this:
JToken parse = JObject.Parse(result3);
JArray msgcol = (JArray)parse["response"]["items"];
foreach (JObject obj in msgcol)
{
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question