I have a C# console app fetching json from a web api. Using Newtonsoft JSON deserialization, I successfully manage to pull the data however I can't access data[0]
for example.
My code:
internal class Program
{
private static void Main()
{ // TODO: check if HttpWebRequest can replace WebRequest.Create
var webRequest = WebRequest.Create("http://api.giphy.com/v1/gifs/search?q=" + "cute cat" + "&api_key=...") as HttpWebRequest;
if (webRequest == null)
{
return;
}
webRequest.ContentType = "application/json";
webRequest.UserAgent = "Nothing";
using (var s = webRequest.GetResponse().GetResponseStream())
{
using (var sr = new StreamReader(s))
{
var Json = sr.ReadToEnd(); // json response from web api request
dynamic data = JsonConvert.DeserializeObject(Json); // json to c# objects
//(data.images).ForEach(Console.WriteLine);
System.Console.WriteLine(data);
}
}
Console.ReadLine();
}
}
but when trying to access data[0]
I get:
System.ArgumentException: 'Accessed JObject values with invalid key value: 0. Object property name expected.'
I also tried without dynamic
with the help of json2csharp that did not work either.