我想从此API中获取以
usd
为单位的价格值,并将其放入一个变量中:
https://api.coingecko.com/api/v3/simple/price?ids=veco&vs_currencies=usd
我已经尝试了这段代码,但是出现了错误:public static void StartGet()
{
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(VECO.VecoPriceURL));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
string jsonString;
using (Stream stream = WebResp.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
jsonString = reader.ReadToEnd();
}
List<VECO.Coin> items = JsonConvert.DeserializeObject<List<VECO.Coin>>(jsonString);
foreach (var item in items)
{
Console.WriteLine(item.usd);
}
}
public class VECO
{
public static string VecoPriceURL = "https://api.coingecko.com/api/v3/simple/price?ids=veco&vs_currencies=usd";
public class Coin
{
public string usd { get; set; }
}
}
错误:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current
JSON object (e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[ConsoleProgram.VECO+Coin]' because the
type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or
change the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or List<T>)
that can be deserialized from a JSON object. JsonObjectAttribute can also be
added to the type to force it to deserialize from a JSON object.
Path 'veco', line 1, position 8.'
public class COIN_NAME
和public class RootObject
。我想要这个方法StartGet(),获取我想要的每个硬币的美元价值。就像我给它比特币的url,从json中获取美元价值以及许多其他硬币。使用上面的代码,我必须为我想要获取其美元价值的每个硬币使用那个StartGet()方法。 - aria darkkkis