Reputation: 61
WebClient client = new WebClient();
Stream stream = client.OpenRead(" some link ");
StreamReader reader = new StreamReader(stream);
Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
List<String> list = new List<string>();
//loading list
for (int i = 0; i < ((string)jObject["some_stream"][i]["some_channel"]["some_name"]).Count(); i++)
{
string result = ((string)jObject["some_streams"][i]["some_channel"]["some_name"]);
list.Insert(i, result);
}
stream.Close();
This code is working, but in json data I have 20+ results should be returned, but I only get 8.
What could be the cause?
Upvotes: 0
Views: 605
Reputation: 21742
you are counting the length of a string. at some point the length of that string is equal to or less than i (the 9th value of the string if you manage to iterate 8 times)
That is this piece of code
((string)jObject["some_stream"][i]["some_channel"]["some_name"]).Count()
returns the length of a string at location i
so if you manage to iterate 8 times then the string at jObject["some_stream"][9]["some_channel"]["some_name"] has a length of 9 or less at which time the looping ends
From the usage it looks like jObject["Some_stream"]
returns an array in that case what you could do is something like this:
var arr = (Treal[])jObject["Some_stream"];
var list = (from obj in arr
select ((string)obj["some_channel"]["some_name"])).ToList();
you will need to substitue TReal with the actual type of jObject["Some_stream"]
aside: when ever you are opening a stream it's a good idea to do this within a using statement. In your code the stream would not be closed in the case of an exception the code would then be
WebClient client = new WebClient();
using(var stream = client.OpenRead(" some link ")) {
reader = new StreamReader(stream);
var jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
var arr = (Treal[])jObject["Some_stream"];
var list = (from obj in arr
select ((string)obj["some_channel"]["some_name"])).ToList();
}
Upvotes: 1