-1

I'm trying to parse this json array: https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=60&aggregate=3&e=Kraken

Idea is read some values for each timestamp(not all needed). How I could data of 1st time-stamp, close, high, low, open?

I've tried several methods I used for simple json stings but this seem to be challenge for me and can't get data read any ways I've tried.

First I made separate class file like this:

namespace HistoryData
{
    class DataQuery
    {
        public string Data { get; set; }
        public string Time { get; set; }
        public string Close { get; set; }
        public string High { get; set; }
        public string Low { get; set; }
        public string Open { get; set; }
        public string Volumefrom { get; set; }
        public string Volumeto { get; set; }
    }
}

Then parse:

const string url = @"https://min-api.cryptocompare.com/data/histominute?
fsym=BTC&tsym=USD&limit=30&aggregate=3&e=Kraken";
var client = new WebClient();
var content = client.DownloadString(url);
var results = JsonConvert.DeserializeObject<List<DataQuery>>(content);
var Time1 = (results[1].Time);
var Close1 = (results[1].Close);

NOT WORKING

dbc
  • 104,963
  • 20
  • 228
  • 340
Lorean
  • 29
  • 4
  • 1
    What problem are you facing, exactly? Read [ask]. – CodeCaster Dec 19 '17 at 11:52
  • 1
    Here are some tips you can google to get you to a point where you can ask a good question. Create a class with properties that match the JSON schema. Use JSON.Net to deserialize the JSON to an object or list. Use that to get your datra. – Crowcoder Dec 19 '17 at 11:52
  • 1
    You mention you have tried different things, can you elaborate on what your approach was? Where are you stuck? – Glubus Dec 19 '17 at 11:53

1 Answers1

0

If you format your JSON with https://jsonformatter.curiousconcept.com/ you will see the outermost container is an object, not an array:

{
   "Response":"Success",
   "Type":100,
   "Aggregated":true,
   "Data": [/*...*/],
   "TimeTo":1513705980,
   "TimeFrom":1513695060,
   "FirstValueInArray":true,
   "ConversionType":{
      "type":"force_direct",
      "conversionSymbol":""
   }
}

Thus you need to deserialize to an appropriate root data model that reflects this JSON structure. Your DataQuery reflects only the nested "Data" objects. You can use http://json2csharp.com/ or Paste JSON as Classes to design that model for you:

public class Datum
{
    public long time { get; set; }
    public decimal close { get; set; }
    public decimal high { get; set; }
    public decimal low { get; set; }
    public decimal open { get; set; }
    public double volumefrom { get; set; }
    public double volumeto { get; set; }
}

public class ConversionType
{
    public string type { get; set; }
    public string conversionSymbol { get; set; }
}

public class RootObject
{
    public string Response { get; set; }
    public int Type { get; set; }
    public bool Aggregated { get; set; }
    public List<Datum> Data { get; set; }
    public long TimeTo { get; set; }
    public long TimeFrom { get; set; }
    public bool FirstValueInArray { get; set; }
    public ConversionType ConversionType { get; set; }
}

And deserialize like:

var root = JsonConvert.DeserializeObject<RootObject>(content);
var results = root.Data;

var Time1 = (results[1].time);
var Close1 = (results[1].close);

Notes:

  • Since close, high, low and open look to be currency values, I modified their types to decimal.

  • I also changed the timestamps to long for safety.

Working .Net fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340