所以我正在使用 c# xamarin 并制作一个基本的股票应用程序,该应用程序可以使用 Alpha Vantage API 提取股票报价。我有我认为可能有用的东西,但它根本不起作用。是的,这是一个学校项目,所以我不希望人们直接去做。这个应用程序的目的是使用 API,我需要在用户从应用程序的第一页输入股票代码后显示它提供的数据。我需要用 api 发送那个符号,我不确定我是否在提取 JSON 对象,而且当我正确检索 JSON 时,我不知道如何到达对象中的每个字段。这段代码是我正在尝试的,我没有将任何信息填充到我的任何 textView 中。
namespace Stock_Quote
{
[Activity(Label = "StockInfoActivity1")]
public class StockInfoActivity1 : Activity
{
private ISharedPreferences prefs = Application.Context.GetSharedPreferences("APP_DATA", FileCreationMode.Private);
TextView txtSymbol, txtOpen, txtClose, txtHigh, txtLow, txtVolume;
string webservice_url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.activity_stock_info);
txtSymbol = FindViewById<TextView>(Resource.Id.txtSymbol);
txtOpen = FindViewById<TextView>(Resource.Id.txtOpen);
txtClose = FindViewById<TextView>(Resource.Id.txtClose);
txtHigh = FindViewById<TextView>(Resource.Id.txtHigh);
txtLow = FindViewById<TextView>(Resource.Id.txtLow);
txtVolume = FindViewById<TextView>(Resource.Id.txtVolume);
string current = prefs.GetString("current", "no stok symbol found");
//txtSymbol.Text = current;
try
{
webservice_url = webservice_url + current + "&apikey=AVALIDAPIKEY";
Uri url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
if (webRequest != null)
{
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
//Get the response
WebResponse wr = webRequest.GetResponseAsync().Result;
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream);
Stock currentStockInfo = JsonConvert.DeserializeObject<Stock>(reader.ReadToEnd());
if (currentStockInfo.RestResponse.result == null)
{
txtSymbol.Text = "No stock found";
}
else
{
txtSymbol.Text = current;
txtOpen.Text = currentStockInfo.RestResponse.stockInfo.Open;
txtClose.Text = currentStockInfo.RestResponse.stockInfo.Close;
txtHigh.Text = currentStockInfo.RestResponse.stockInfo.High;
txtLow.Text = currentStockInfo.RestResponse.stockInfo.Low;
txtVolume.Text = currentStockInfo.RestResponse.stockInfo.Volume;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public class Result
{
public string Information { get; set; }
public string Symbol { get; set; }
public string Last { get; set; }
public string Size { get; set; }
public string TimeZone { get; set; }
}
public class StockInfo
{
public string Open { get; set; }
public string High { get; set; }
public string Low { get; set; }
public string Close { get; set; }
public string Volume { get; set; }
}
public class RestResponse
{
public Result result { get; set; }
public StockInfo stockInfo { get; set; }
}
public class Stock
{
public RestResponse RestResponse { get; set; }
}
}
最佳答案
从该端点返回的 JSON 与您的模型不完全匹配。
这是告诉程序如何解析响应的行:
股票 currentStockInfo = JsonConvert.DeserializeObject(reader.ReadToEnd());
...但是响应看起来像这样:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-12-10 16:00:02",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-12-10": {
"1. open": "104.8000",
"2. high": "107.9800",
"3. low": "103.8900",
"4. close": "107.5900",
"5. volume": "39050766"
},
"2018-12-07": {
"1. open": "108.3800",
"2. high": "109.4500",
"3. low": "104.3000",
"4. close": "104.8200",
"5. volume": "45044937"
}...
...
...
...
您试图将整个响应转换为一个 Stock
对象,但这是行不通的。响应不是 Stock
,它是一个包含 2 个对象的响应,其中一个包含很多 Stock
对象。
您可以尝试为此创建模型*,但我建议将整个响应转换为 JObject
(NewtonSoft JSON.Net 中的另一个对象)。
这是我放在一起的 DotNetFiddle 来演示它是如何工作的。
https://dotnetfiddle.net/Iz8UsD
让我知道是否可以添加更有帮助的内容。
编辑:* 您可能会在这里使用强类型模型,问题是每个 Stock
都有不同的 JSON 名称。我不知道如何让解析器将每个解析为 Stock
,同时仍保留数据。我今晚必须玩这个。
关于c# - 我正在使用 Alpha Vantage API 来尝试获取每日股票信息。我对使用 API 很陌生,不知道自己做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53713335/