C# 从API中获取数字并将其放入变量中
我想得到这个api中的价格值,它是C# 从API中获取数字并将其放入变量中,c#,asp.net,json,asp.net-core,C#,Asp.net,Json,Asp.net Core,我想得到这个api中的价格值,它是usd,并将其放入一个变量中: 我已尝试此代码,但出现错误: public static void StartGet() { HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(VECO.VecoPriceURL)); WebReq.Method = "GET"; HttpWebResponse WebResp
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; }
}
}
publicstaticvoidstartget()
{
HttpWebRequest WebReq=(HttpWebRequest)WebRequest.Create(string.Format(VECO.VecoPriceURL));
WebReq.Method=“GET”;
HttpWebResponse WebResp=(HttpWebResponse)WebReq.GetResponse();
字符串jsonString;
使用(Stream-Stream=WebResp.GetResponseStream())
{
StreamReader=新的StreamReader(stream,System.Text.Encoding.UTF8);
jsonString=reader.ReadToEnd();
}
List items=JsonConvert.DeserializeObject(jsonString);
foreach(项目中的var项目)
{
控制台写入线(项目美元);
}
}
公共类维科
{
公共静态字符串VecoPriceURL=”https://api.coingecko.com/api/v3/simple/price?ids=veco&vs_currencies=usd";
公共级硬币
{
公共字符串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.'
Newtonsoft.Json.JsonSerializationException:'无法反序列化当前
JSON对象(例如{“名称”:“值”})转换为类型
'System.Collections.Generic.List`1[ConsoleProgram.VECO+Coin]'因为
类型需要JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或
更改反序列化类型,使其成为正常的.NET类型(例如,不是
基本类型(如整数,而不是数组或列表等集合类型)
可以从JSON对象反序列化的。JsonObjectAttribute也可以是
添加到类型以强制其从JSON对象反序列化。
路径“veco”,第1行,位置8
您的数据结构需要略有不同
public class Veco
{
public decimal usd { get; set; }
}
public class RootObject
{
public Veco veco { get; set; }
}
请注意,Json不是数组或列表,因此还需要JsonConvert.DeserializeObject方法中的列表。相反,您需要执行以下操作
var result = JsonConvert.DeserializeObject<RootObject>(jsonString);
重写你的方法
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();
}
var item = JsonConvert.DeserializeObject<RootObject>(jsonString);
Console.WriteLine(item.veco.usd);
}
输出
USD Rate 0.01558532
For veco,USD Rate : 0.01581513
For eos,USD Rate : 2.42
For uraniumx,USD Rate : 0.890397
@ariadarkkkis很高兴帮助你。如果有助于您解决问题,请将其标记为答案。我如何使用此行
var result=JsonConvert.DeserializeObject(jsonString)
如果我想使用StartGet()
作为方法。像StartGet(字符串url,…)
一样,为了获取和打印结果,您可以执行var items=JsonConvert.DeserializeObject(jsonString);如果你仍然没有得到它,我可以用你修改过的StartGet方法更新我的答案。让我知道,我知道。但我想在这个应用程序中添加更多的硬币,比如比特币和其他一些硬币。正如您所说,它们中的每一个都有一个公共类COIN\u NAME
和公共类RootObject
。我想要这个方法StartGet(),得到我想要的每一枚硬币的美元价值。比如我给它一个比特币url,然后从json和许多其他硬币中获取美元值。在上面的代码中,我必须使用StartGet()来计算每一枚硬币的美元价值。
public static void StartGet(string id)
{
var url = $"https://api.coingecko.com/api/v3/simple/price?ids={id}&vs_currencies=usd";
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
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();
}
var result = JsonConvert.DeserializeObject<JToken>(jsonString);
Console.WriteLine($"For {id},USD Rate : {result[id].Value<string>("usd")}");
}
StartGet("veco");
StartGet("eos");
StartGet("uraniumx");
For veco,USD Rate : 0.01581513
For eos,USD Rate : 2.42
For uraniumx,USD Rate : 0.890397