1

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 ..

2
  • do you have a c# object which represents the Market json? Commented Jun 29, 2017 at 22:37
  • 3
    Possible duplicate of Easiest way to parse JSON response Commented Jun 29, 2017 at 22:38

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

9 Comments

oh thanks , but also if i want to compare the current volume with previous volume i stored in "X" variable for each market , do you have an idea about how to do it ?
declare a variable to hold the volume value above the foreach loop and compare on every iteration of the foreach loop, skipping the first iteration with a null check.
What is your goal? Do you want the maximum volume or minimum?
I'm getting an error :[ Result : cannot reference a type through an expression. try Coin.Result]
Thanks for you help !! :) i got the idea but didn't test the code :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.