Why does this Dynamic object show Count as 0 even if it contains information?

EDIT 2: I’m an idiot, explanation below.

Hi! Novice question.

I came across a problem I couldn’t figure out. I have a function which prints the name of an item, it’s overall price, and buying price to console. The values are stored in a json object.

The problem bit (part of another function):

dynamic JsonPriceObject = GetItemPrice(Int32.Parse(tempItemID)); for (int y = 0; y < JsonPriceObject.Count; i++) { string overall = JsonPriceObject[i].Overall; string buying = JsonPriceObject[i].buying; Console.WriteLine("Item: " + itemName); Console.WriteLine("Overall price: " + overall); Console.WriteLine("Buying price: " + buying); } 

And the method which returns the dynamic object:

private static dynamic GetItemPrice(int itemID) { string queryURL = GuidePriceURL + itemID; WebClient client = new WebClient(); string price = client.DownloadString(queryURL); dynamic JsonObject = JsonConvert.DeserializeObject(price); return JsonObject; } 

The problem I have is that in the first function “JsonPriceObject.Count” is somehow 0, and so never enters the loop in the first place. I added a line “Console.WriteLine(JsonPriceObject);” outside of the loop and it returns the Json object as expected, in Json format and everything.

How come the .Count returns 0 even when it does contain information?

EDIT: I’m using Newtonsoft’s JSON.NET here.

Edit 2: First I noticed that the string “Price” doesn’t include the square brackets “[ { – data – } ]”, so I went ahead and added those. As I re-read the loop I’ve written, I noticed that I used the wrong variable. Int i is used further up in my code, inside another loop. Fixed those to the correct variable (int y), and now it works fine.

submitted by /u/Zecuel
[link] [comments]

Leave a Reply