问题描述
我正在使用 Xamarin.Forms 进行股票监视列表项目。在我使用 IEX Cloud API 之前收集股票价格,但我正在尝试使用 Yahoo Finance API。我使用以下代码,但 result
返回空白。
namespace Stock_WatchList
{
public partial class MainPage : ContentPage
{
public string ApplePrice { get; set; }
public string NioPrice { get; set; }
public string PalantirPrice { get; set; }
public string TeslaPrice { get; set; }
public string XpengPrice { get; set; }
public string AmazonPrice { get; set; }
public string PinduoduoPrice { get; set; }
public string disneyPrice { get; set; }
public string AmdPrice { get; set; }
public string TwitterPrice { get; set; }
public MainPage()
{
InitializeComponent();
WebClient yahoo = new WebClient();
string yahooPrices = yahoo.DownloadString("https://query1.finance.yahoo.com/v7/finance/chart/AAPL?interval=5m ");
MatchCollection YahooMatches = System.Text.RegularExpressions.Regex.Matches(yahooPrices,@"""([^""]+)"":{""quote"":{""latestPrice"":(\d+(?:.\d+))}}");
foreach (Match match in YahooMatches)
{
switch (match.Groups[1].Value)
{
case "regularMarketPrice":
ApplePrice = $"$ {match.Groups[2].Value}";
break;
}
}
BindingContext = this;
}
private async void ImageButton_Clicked(object sender,EventArgs e)
{
await Navigation.PushAsync(new SearchPage());
}
}
}
谁能帮我解决这个问题?
解决方法
我使用 Postman 调用了 yahoo api 并以 json 格式接收了数据。我能够轻松地从该程序中查看数据,我同意 Jason 的观点,在这种情况下,JSON 解析器会为您提供更好的服务。