My market API service discontinued the call I was using to grab data into my C# application. They have since released a "batch quote" feature that could actually save lot of work in my application. It returns the data in JSON. I'm using Newtonsoft to parse my old API call, which returns a very predictable output. However, now the batch quote API is returning an dynamic number of stocks at once, I can't quite crack the syntax.
JSON output:
{
"Meta Data": {
"1. Information": "Batch Stock Market Quotes",
"2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
"3. Time Zone": "US/Eastern"
},
"Stock Quotes": [
{
"1. symbol": "MSFT",
"2. price": "96.1800",
"3. volume": "20087326",
"4. timestamp": "2018-03-09 13:53:07"
},
{
"1. symbol": "AMD",
"2. price": "11.6800",
"3. volume": "63025764",
"4. timestamp": "2018-03-09 13:53:08"
},
{
"1. symbol": "NVDA",
"2. price": "243.9600",
"3. volume": "8649187",
"4. timestamp": "2018-03-09 13:52:51"
}
]
}
So the "Stock Quotes" array is what I need to grab every time, and the number of elements within the array are dependent upon in the input by user.
So far I've tried:
dynamic obj = JsonConvert.DeserializeObject(rawJSON);
Which seems to create some generic object with 2 children. I need the all the children of the second child, "Stock Quotes". The object contains a count method that shows how many child elements "Stock Quotes" has. I'm just not sure of the syntax I need to use to grab each child element. The goal is to throw each child element into a dictionary of objects of a custom class.
Any help is greatly appreciated, thank you!
Note: All else fails I'll probably be manipulating strings for the square brackets containing the "Stock Quotes" children, then parsing those, and so on. Quick and dirty, and it will probably work - but there's got to be a more elegant method, right?
You can do this by creating classes for each set and specifying the field names that come in the JSON data with [JsonProperty(PropertyName = "Field name")]
One advantage of using classes instead of dynamic
is that you get Intellisense.
So, your DTO classes could look something like this:
public class Data
{
[JsonProperty(PropertyName = "Meta Data")]
public Metadata Metadata { get; set; }
[JsonProperty(PropertyName = "Stock Quotes")]
public IList<StockQuote> StockQuotes { get; set; }
}
public class Metadata
{
[JsonProperty(PropertyName = "1. Information")]
public string Information { get; set; }
[JsonProperty(PropertyName = "2. Notes")]
public string Notes { get; set; }
[JsonProperty(PropertyName = "3. Time Zone")]
public string Timezone { get; set; }
}
public class StockQuote
{
[JsonProperty(PropertyName = "1. symbol")]
public string Symbol { get; set; }
[JsonProperty(PropertyName = "2. price")]
public string Price { get; set; }
[JsonProperty(PropertyName = "3. volume")]
public string Volume { get; set; }
[JsonProperty(PropertyName = "4. timestamp")]
public string Timestamp { get; set; }
}
Then you just:
var data = JsonConvert.DeserializeObject<Data>(str);