简体   繁体   English

转换列表<objects>字典<string, string>在 C#</string,></objects>

[英]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());

转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在c#中将包含数组和字典的字符串转换为列表,反之亦然 - Convert the string containing array and dict into the list and vice versa in c# 将JSON字符串转换为C#中的动态对象列表 - Convert JSON String to List of Dynamic Objects in C# 将制表符分隔数据的字符串转换为C#中的对象列表 - Convert string of tab seperated data into list of objects in C# 将字符串转换为列表 - C# - Convert String to List - c# 在 C# 中将列表转换为字符串 - Convert a list to a string in C# c#将json字符串的一部分转换为对象 - c# convert a part of a json string to objects C#转换列表 <string> 列出 <TimeSpan> - C# Convert List<string> to List<TimeSpan> 将json字符串转换为c#对象列表(字符串来自请求,为NULL) - convert json string to c# list of objects (string comes from request as NULL) 转换列表 <string> 列表 <int> 在C#中 - Convert List<string> to List<int> in C# 转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM