0

I am currently trying to parse the JSON data that I get back from the IEX api, which consists of stocks and their information. The problem I am running into is that depending on which stocks/symbols (ie AAPL, GOOGL etc) you request the keys that are returned change to that symbol. An example of the returned JSON can be seen here: https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb,googl&types=quote,chart&range=1m

To deserialize this I am using JSON.NET and the following classes:

public class Stock  {
    public quote Quote { get; set;}
    public chart Chart { get; set;}
}

public class Root {
    public Stock[] Stock;
}

And then I also have classes for the quote and chart objects. I then deserialize the JSON using:

var stocks =  JsonConvert.DeserializeObject<Root>(jsonstring);

However, this does not work, but this does work when renaming the 'Stock' class to one of the symbol names, but then only that symbols JSON is parsed. I have no idea what's going on here so any help is greatly appreciated!

Thijs van der Heijden
  • 1,147
  • 1
  • 10
  • 25
  • Since you're using iextrading, have you considered using a library already built so that you don't have to build structures? see https://www.codepoc.io/blog/web-api/5297/get-stock-historical-data-based-on-it-stock-symbol-iextrading-api-c – tatmanblue Dec 12 '18 at 21:05
  • Deserialize to a `Dictionary` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182) or [Parsing JSON Object with variable properties into strongly typed object](https://stackoverflow.com/q/34202496). – dbc Dec 12 '18 at 21:43
  • You can create your classes like follows: public class AAPL { [JsonProperty("quote")] public Quote Quote; } public class Root { [JsonProperty("AAPL")] public AAPL Appl; } public class Quote { [JsonProperty("symbol")] public string Symbol; // other properties .. } – GoldenAge Dec 12 '18 at 21:59
  • The method which gets the data from your API: static async Task getStocks() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,fb,googl&types=quote,chart&range=1m"); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } – GoldenAge Dec 12 '18 at 22:00
  • I've created a simple console app, you can make conversion like this in the Main method: static void Main(string[] args) { var data = getStocks().GetAwaiter().GetResult(); var res = JsonConvert.DeserializeObject(data); // another method to get deserialized data dynamic stuff = JsonConvert.DeserializeObject(data); var obj1 = stuff.AAPL; var obj2 = stuff.AAPL.quote; var obj3 = stuff.AAPL.quote.symbol; } – GoldenAge Dec 12 '18 at 22:02
  • I'm not able to add my answer cuz the question has been blocked but hope it helps. Cheers – GoldenAge Dec 12 '18 at 22:03

1 Answers1

2

Your problem is that your class models don't match the json schema of the API output. The output isn't a list of stocks, it is a mapping of StockName: Stock

You can deserialize it as a Dictionary<string, Stock> rather than a Root and that should get you where you need to be.

Dan
  • 858
  • 7
  • 18