【问题标题】:JSON deserialize c# get values from classJSON反序列化c#从类中获取值
【发布时间】:2021-04-09 21:17:38
【问题描述】:

我正在尝试反序列化文件。

我的文件 plik.json 看起来像:https://api.jsonstorage.net/v1/json/2fc690dd-fe2e-40cc-b7ce-a34b8326ed33

这是我的代码:

 public class Filter
    {
        public string filterType { get; set; }
        public string minPrice { get; set; }
        public string maxPrice { get; set; }
        public string tickSize { get; set; }
        public string multiplierUp { get; set; }
        public string multiplierDown { get; set; }
        public int? avgPriceMins { get; set; }
        public string minQty { get; set; }
        public string maxQty { get; set; }
        public string stepSize { get; set; }
        public string minNotional { get; set; }
        public bool? applyToMarket { get; set; }
        public int? limit { get; set; }
        public int? maxNumOrders { get; set; }
        public int? maxNumAlgoOrders { get; set; }
    }

    public class Symbol
    {
        public string symbol { get; set; }
        public string status { get; set; }
        public string baseAsset { get; set; }
        public int baseAssetPrecision { get; set; }
        public string quoteAsset { get; set; }
        public int quotePrecision { get; set; }
        public int quoteAssetPrecision { get; set; }
        public int baseCommissionPrecision { get; set; }
        public int quoteCommissionPrecision { get; set; }
        public List<string> orderTypes { get; set; }
        public bool icebergAllowed { get; set; }
        public bool ocoAllowed { get; set; }
        public bool quoteOrderQtyMarketAllowed { get; set; }
        public bool isSpotTradingAllowed { get; set; }
        public bool isMarginTradingAllowed { get; set; }
        public List<Filter> filters { get; set; }
        public List<string> permissions { get; set; }
    }

    public class Root
    {
        public List<Symbol> symbols { get; set; }
    }

    private void button29_Click(object sender, EventArgs e)
    {

        using (StreamReader r = new StreamReader("C:\\Users\\Adamsz\\Pictures\\plik.json"))
        {
            Console.WriteLine("Before");
            string json = r.ReadToEnd();
           // Console.WriteLine(json);
            Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(json);
            Console.WriteLine("After");

        }

        
           List<Symbol> xd = new List<Symbol>();
        foreach (var m in xd)
        {
            Console.WriteLine(m.symbol.ToString());
            List <Filter> filtr = new List<Filter>();
            foreach (var x in filtr)
            {
                if (x.filterType == "LOT_SIZE")
                {
                    Console.WriteLine(x.minQty);
                }
                   
            }
        }
       

    }

现在我遇到了 WrtieLine 的问题。我应该在我的代码中进行哪些更改才能获得准确的输出?提前致谢! 输出应该是这样的:

ETHBTC

0.00100000

LTCBTC

0.01000000

【问题讨论】:

  • 为什么你期望xd = new List&lt;Symbol&gt;() 有一些元素? xd 应该取自您反序列化的 Root 实例
  • @Selvin 你能告诉我如何从 Root 中获得这些值吗?
  • 通过访问其唯一属性并分配给变量
  • 如果可以并且知道具体方法,请填写答案。
  • 我知道,但有时很难做点什么。我在这里需要一些帮助...

标签: c# json serialization


【解决方案1】:
 List<Symbol> symbols = myDeserializedClass.symbols;
            foreach (var m in symbols)
            {

                Console.WriteLine(m.symbol.ToString());
                List<Filter> filtr = m.filters;
                foreach (var x in filtr)
                {
                    if(x.filterType == "LOT_SIZE")
                    Console.WriteLine(x.minQty);
                }
            }

这就是我要找的……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多