[英]Convert List<objects> to a Dict <String, String> in C#
I want to create an app that shows me the value of a certain coin.我想创建一个应用程序来显示某种硬币的价值。 I use KuCoin API.我使用库币 API。 When I request pricing info, i get this response:当我请求定价信息时,我收到以下回复:
{
"code": "200000",
"data_fiat": {
"LOKI": "0.39646657",
"EXY": "0.01427250",
"IOTX": "0.00411990",
"MHC": "0.0035",
"MXW": "0.14063173",
...
}
}
I used Deserialze, and a Foreach loop to convert this to a List of data_fiat objects.我使用 Deserialze 和 Foreach 循环将其转换为 data_fiat 对象列表。 Now, when I want to get a value I just type data_fiat.LOKI.现在,当我想获得一个值时,我只需键入 data_fiat.LOKI。
The problem is that I would need to create an IF for every possible coin (200+) and its ugly code.问题是我需要为每个可能的硬币(200+)及其难看的代码创建一个 IF。 Is there a way to somehow convert this to a DICTIONARY, so I can use data_fiatDict[VAR]?有没有办法以某种方式将其转换为字典,以便我可以使用 data_fiatDict[VAR]?
This is what I have now:这就是我现在所拥有的:
public static async Task<List<data_fiat>> GetPrices()
{
FiatPrices data = new FiatPrices();
List<data_fiat> price = new List<data_fiat>();
string url = $"https://api.kucoin.com/api/v1/prices";
using (HttpClient client = GetHttpClient())
{
try
{
string json = await client.GetStringAsync(url);
data = JsonConvert.DeserializeObject<FiatPrices>(json);
foreach (data_fiat item in data.price)
{
price.Add(item);
}
}
catch (Exception ex)
{
throw ex;
}
}
return price;
}
This returns a LIST of data_fiat这将返回 data_fiat 的列表
private async Task GenerateData(Coins Selection)
{
Coindata.BindingContext = Selection;
String CoinSelection = Selection.Name;
pricelbl.Text = ??Dictionary??[CoinSelection];
}
So I need a dictionary or something like that to be in the??Dictionary??所以我需要一本字典或类似的东西在??字典?? spot.点。
If you declare the FiatPrices
class with a dictionary, JsonConvert will automatically deserialize into this dictionary.如果您使用字典声明FiatPrices
类,JsonConvert 将自动反序列化到该字典中。
public class FiatPrices
{
[JsonProperty("code")]
public int Code { get; set; }
[JsonProperty("data_fiat")]
public Dictionary<string, decimal> Prices { get; set; }
}
Then you can deserialize and query with然后你可以反序列化和查询
FiatPrices data = JsonConvert.DeserializeObject<FiatPrices>(json);
decimal price = data.Prices["IOTX"];
If you only need the dictionary, you can extract it with a single assigment (no need to loop):如果你只需要字典,你可以用一个单一的分配来提取它(不需要循环):
Dictionary<string, decimal> priceDict = data.Prices;
or in your GetPrices
method with an adapted return type of Task<Dictionary<string, decimal>>
:或在您的GetPrices
方法中使用经过调整的返回类型Task<Dictionary<string, decimal>>
:
return data.Prices;
You can make use of JObject
and select the node you need, then deserialize it to Dictionary<string,string>
.您可以使用JObject
并选择所需的节点,然后将其反序列化为Dictionary<string,string>
。
so instead of doing this:所以不要这样做:
data = JsonConvert.DeserializeObject<FiatPrices>(json);
foreach (data_fiat item in data.price)
{
price.Add(item);
}
you could do this:你可以这样做:
JObject jObj = JObject.Parse(json);
var data_dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObj["data_fiat"].ToString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.