这是我要解析的JSON文件
"success" : true,
"message" : "",
"result" : [{
"MarketName" : "BTC-888",
"High" : 0.00000919,
"Low" : 0.00000820,
"Volume" : 74339.61396015,
"Last" : 0.00000820,
"BaseVolume" : 0.64966963,
"TimeStamp" : "2014-07-09T07:19:30.15",
"Bid" : 0.00000820,
"Ask" : 0.00000831,
"OpenBuyOrders" : 15,
"OpenSellOrders" : 15,
"PrevDay" : 0.00000821,
"Created" : "2014-03-20T06:00:00",
"DisplayMarketName" : null
}, {
"MarketName" : "BTC-A3C",
"High" : 0.00000072,
"Low" : 0.00000001,
"Volume" : 166340678.42280999,
"Last" : 0.00000005,
"BaseVolume" : 17.59720424,
"TimeStamp" : "2014-07-09T07:21:40.51",
"Bid" : 0.00000004,
"Ask" : 0.00000005,
"OpenBuyOrders" : 18,
"OpenSellOrders" : 18,
"PrevDay" : 0.00000002,
"Created" : "2014-05-30T07:57:49.637",
"DisplayMarketName" : null
}
]
如何解析它以获得任何市场名称的“Volume”和“LastPrice”?有超过64个市场,那么最好的方法是什么? 另外我想比较旧音量和新音量..
答案 0 :(得分:2)
您可以使用此课程:
public class Result
{
public string MarketName { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Volume { get; set; }
public double Last { get; set; }
public double BaseVolume { get; set; }
public string TimeStamp { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public int OpenBuyOrders { get; set; }
public int OpenSellOrders { get; set; }
public double PrevDay { get; set; }
public string Created { get; set; }
public object DisplayMarketName { get; set; }
}
public class MyClass
{
public bool success { get; set; }
public string message { get; set; }
public List<Result> result { get; set; }
}
然后您可以从结果列表中获取“音量”和“最后”:
string YourJsonString = ".....";
var myClass = new JavaScriptSerializer().Deserialize<MyClass>(YourJsonString);
foreach(var result in myClass.Result){
Console.WriteLine("Volume: " + result.Volume);
Console.WriteLine("Last: " + result.Last);
}
我没有测试过这段代码,但它应该可行。如果不发表评论。
根据评论更新:
如果您想将一个市场与下一个市场进行比较,您可以使用for循环而不是foreach:
for(int i=0; i<myClass.result.Count;i++){
if(myClass.Result[i].Volume > myClass.result[i+1].Volume)
{
Console.WriteLine("Volume "+i+": "+myClass.result[i].Volume+" is higher then volume "+(i+1)+": " + myClass.result[i+1].Volume);
}
if(myClass.result[i].Last > myClass.result[i+1].Last)
{
Console.WriteLine("Last "+i+": "+myClass.result[i].Last +" is higher then last "+(i+1)+": " + myClass.result[i+1].Last );
}
}
UPDATE2:
比较的另一种方法是:
声明一个全局字段:
private List<Result> _oldResult = new List<Result>();
之后,执行此操作:
foreach(var result in myClass.Result){
// do something with the list
// compare only, if oldResult is not empty
if(oldResult.Count == 0) continoue;
foreach(var oldResult in _oldResult ){
if(result.MarketName.Equals(oldResult.MarketName)){
if(result.Volume > oldResult.Volume)
{
Console.WriteLine("Volume: " + result.Volume);
Console.WriteLine("Volume: " + oldResult .Volume);
}
break;
}
}
}
_oldResult = myClass.Result;