JSON反序列化c#从类中获取值
我正在尝试反序列化文件 我的文件plik.json如下所示: 这是我的代码:JSON反序列化c#从类中获取值,c#,json,serialization,C#,Json,Serialization,我正在尝试反序列化文件 我的文件plik.json如下所示: 这是我的代码: public class Filter { public string filterType { get; set; } public string minPrice { get; set; } public string maxPrice { get; set; } public string tickSize { get; set; }
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);
}
}
}
}
公共类过滤器
{
公共字符串筛选器类型{get;set;}
公共字符串minPrice{get;set;}
公共字符串maxPrice{get;set;}
公共字符串大小{get;set;}
公共字符串乘法器{get;set;}
公共字符串乘法器向下{get;set;}
公共int?avgPriceMins{get;set;}
公共字符串minQty{get;set;}
公共字符串maxQty{get;set;}
公共字符串步长{get;set;}
公共字符串minNotional{get;set;}
公共布尔?applyToMarket{get;set;}
公共整数?限制{get;set;}
公共int?maxNumOrders{get;set;}
公共int?maxNumAlgoOrders{get;set;}
}
公共类符号
{
公共字符串符号{get;set;}
公共字符串状态{get;set;}
公共字符串baseAsset{get;set;}
public int baseAssetPrecision{get;set;}
公共字符串quoteAsset{get;set;}
public int quotePrecision{get;set;}
public int quoteasetprecision{get;set;}
public int baseCommissionPrecision{get;set;}
public int quoteCommissionPrecision{get;set;}
公共列表医嘱类型{get;set;}
公共布尔值允许{get;set;}
公共布尔值{get;set;}
公共bool quoteOrderQtyMarketAllowed{get;set;}
公共布尔值允许{get;set;}
公共布尔是允许的{get;set;}
公共列表筛选器{get;set;}
公共列表权限{get;set;}
}
公共类根
{
公共列表符号{get;set;}
}
私有无效按钮29_单击(对象发送者,事件参数e)
{
使用(StreamReader r=newstreamreader(“C:\\Users\\Adamsz\\Pictures\\plik.json”))
{
控制台。写入线(“之前”);
字符串json=r.ReadToEnd();
//Console.WriteLine(json);
根myDeserializedClass=JsonConvert.DeserializeObject(json);
控制台。写入线(“之后”);
}
List xd=新列表();
foreach(xd中的var m)
{
Console.WriteLine(m.symbol.ToString());
列表过滤器=新列表();
foreach(过滤器中的变量x)
{
如果(x.filterType==“批次大小”)
{
控制台写入线(x.minQty);
}
}
}
}
现在我对WrtieLine有一个问题。我应该在我的代码中做些什么更改来获得这个输出?提前谢谢!
输出应如下所示:
ETHBTC
0.00100000
LTCBTC
0.01000000列出符号=myDeserializedClass.symbols;
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);
}
}
foreach(符号中的var m)
{
Console.WriteLine(m.symbol.ToString());
列表过滤器=m个过滤器;
foreach(过滤器中的变量x)
{
如果(x.filterType==“批次大小”)
控制台写入线(x.minQty);
}
}
这就是我一直在寻找的…为什么希望
xd=new List()
有一些元素xd
应该取自您所使用的Root
实例deserialize@Selvin你能告诉我如何从根中得到这个值吗?通过访问它唯一的属性和变量,如果可以,请写下答案,如果你知道具体的方法。我知道,但有时很难做出一些事情。我需要一些帮助。。。