简体   繁体   中英

Parsing JSON file with multiple keys and different values C#

This is the JSON file that I want to parse

"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
    }
]

How can I parse it to get the "Volume" and "LastPrice" of any marketname existed? There are more than 64 markets, so what is the best way to get it? Also i want to compare between old volume and new volume ..

You can use this class:

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; }
}

and then you can get the "Volume" and "Last" from the Result list:

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);
}

I have not tested this code, but it should work. If not make a comment.


Update based on the comment:

If you want to compare one market with the next market, you can use a for loop instead of 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:

Another way to compare is this:

Declare a global field:

private List<Result> _oldResult = new List<Result>();

After that, do this:

 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM