Reputation: 319
1st time using World Trading data and JSONs in general. I am trying to retrieve the current price of a stock in C#
using Newtonsoft.Json.Linq;
static void get_stock_price()
{
string json;
using (var web = new WebClient())
{
var url = $" https://www.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=Io4R1hqd0rLONZcMp6tJgupqjVEyO0pVJAo65a6QlJjHjVBpEyt5nm73zZ5X";
json = web.DownloadString(url);
//How can I extract the price from the above?
}
According to the developers documentation: the object returns a json structured like below:
{
"symbols_requested": 1,
"symbols_returned": 1,
"data": [
{
"symbol": "AAPL",
"name": "Apple Inc.",
"price": "174.33",
"currency": "USD",
"price_open": "173.71",
"day_high": "175.30",
"day_low": "173.17",
"52_week_high": "233.47",
"52_week_low": "142.00",
"day_change": "0.10",
"change_pct": "0.06",
"close_yesterday": "174.23",
"market_cap": "822014771033",
"volume": "3171",
"volume_avg": "28795902",
"shares": "4715280000",
"stock_exchange_long": "NASDAQ Stock Exchange",
"stock_exchange_short": "NASDAQ",
"timezone": "EST",
"timezone_name": "America/New_York",
"gmt_offset": "-18000",
"last_trade_time": "2019-02-26 16:00:01"
}
]
}
This is the site documentation: https://www.worldtradingdata.com/documentation#stock-and-index-real-time
This is the HTTP request link: https://www.worldtradingdata.com/api/v1/stock?symbol=AAPL,MSFT,HSBA.L&api_token=Io4R1hqd0rLONZcMp6tJgupqjVEyO0pVJAo65a6QlJjHjVBpEyt5nm73zZ5X
Could someone please guide me how to extract the price from the object?
Upvotes: 1
Views: 1419
Reputation: 18973
You can use Newtonsoft.Json library to get price like this:
var trade = JsonConvert.DeserializeObject<dynamic>(json);
var price = trade.data[0].price.ToString();
Upvotes: 2