在C#中解析Cryptocompare API Json数据

时间:2017-09-09 19:32:05

标签: c# cryptography ethereum

我正在从https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=ETH&limit=30&aggregate=1&e=CCCAGG

加载数据

这是回报:

{"Response":"Success","Type":100,"Aggregated":false,"Data":[{"time":1504979760,"high":14.26,"low":14.26,"open":14.26,"volumefrom":4.8,"volumeto":68.49,"close":14.26},{"time":1504979820,"high":14.26,"low":14.27,"open":14.26,"volumefrom":2.57,"volumeto":36.77,"close":14.27},{"time":1504979880,"high":14.26,"low":14.27,"open":14.27,"volumefrom":7.41,"volumeto":105.66,"close":14.26},{"time":1504979940,"high":14.26,"low":14.26,"open":14.26,"volumefrom":3.69,"volumeto":52.61,"close":14.26},{"time":1504980000,"high":14.26,"low":14.27,"open":14.26,"volumefrom":11.23,"volumeto":160.79,"close":14.27},{"time":1504980060,"high":14.27,"low":14.28,"open":14.27,"volumefrom":7.29,"volumeto":104.76,"close":14.27},{"time":1504980120,"high":14.27,"low":14.27,"open":14.27,"volumefrom":4.75,"volumeto":67.76,"close":14.27},{"time":1504980180,"high":14.25,"low":14.28,"open":14.27,"volumefrom":8.25,"volumeto":117.58,"close":14.26},{"time":1504980240,"high":14.26,"low":14.27,"open":14.26,"volumefrom":12.54,"volumeto":178.73,"close":14.26},{"time":1504980300,"high":14.26,"low":14.26,"open":14.26,"volumefrom":7.65,"volumeto":109.08,"close":14.26},{"time":1504980360,"high":14.26,"low":14.26,"open":14.26,"volumefrom":6.88,"volumeto":98.26,"close":14.26},{"time":1504980420,"high":14.25,"low":14.26,"open":14.26,"volumefrom":9.28,"volumeto":132.44,"close":14.25},{"time":1504980480,"high":14.25,"low":14.26,"open":14.25,"volumefrom":80.94,"volumeto":1148.05,"close":14.26},{"time":1504980540,"high":14.22,"low":14.26,"open":14.26,"volumefrom":15.4,"volumeto":219.62,"close":14.26},{"time":1504980600,"high":14.24,"low":14.26,"open":14.26,"volumefrom":7.68,"volumeto":109.25,"close":14.24},{"time":1504980660,"high":14.22,"low":14.25,"open":14.24,"volumefrom":11.51,"volumeto":163.89,"close":14.23},{"time":1504980720,"high":14.23,"low":14.24,"open":14.23,"volumefrom":10.4,"volumeto":147.78,"close":14.24},{"time":1504980780,"high":14.23,"low":14.24,"open":14.24,"volumefrom":2.26,"volumeto":32.23,"close":14.24},{"time":1504980840,"high":14.23,"low":14.24,"open":14.24,"volumefrom":11.45,"volumeto":162.77,"close":14.24},{"time":1504980900,"high":14.23,"low":14.25,"open":14.24,"volumefrom":5.08,"volumeto":72.42,"close":14.24},{"time":1504980960,"high":14.24,"low":14.25,"open":14.24,"volumefrom":7.28,"volumeto":103.57,"close":14.24},{"time":1504981020,"high":14.24,"low":14.25,"open":14.24,"volumefrom":2.22,"volumeto":31.73,"close":14.25},{"time":1504981080,"high":14.25,"low":14.26,"open":14.25,"volumefrom":19.34,"volumeto":275.78,"close":14.26},{"time":1504981140,"high":14.26,"low":14.27,"open":14.26,"volumefrom":13.05,"volumeto":186.2,"close":14.27},{"time":1504981200,"high":14.27,"low":14.28,"open":14.27,"volumefrom":4.95,"volumeto":70.65,"close":14.27},{"time":1504981260,"high":14.26,"low":14.27,"open":14.27,"volumefrom":3.1,"volumeto":44.26,"close":14.27},{"time":1504981320,"high":14.26,"low":14.27,"open":14.27,"volumefrom":8.56,"volumeto":122.21,"close":14.27},{"time":1504981380,"high":14.27,"low":14.27,"open":14.27,"volumefrom":10.8,"volumeto":154.01,"close":14.27},{"time":1504981440,"high":14.27,"low":14.27,"open":14.27,"volumefrom":4.18,"volumeto":59.67,"close":14.27},{"time":1504981500,"high":14.27,"low":14.27,"open":14.27,"volumefrom":0.638,"volumeto":9.11,"close":14.27},{"time":1504981560,"high":14.27,"low":14.27,"open":14.27,"volumefrom":0,"volumeto":0,"close":14.27}],"TimeTo":1504981560,"TimeFrom":1504979760,"FirstValueInArray":true,"ConversionType":{"type":"invert","conversionSymbol":""}}

我想用c#解析数据。主要对每个“数据”列表条目(时间,高,低,......)执行某些操作 有什么建议吗?

编辑:

我安装了这个json包。这是代码。

    string url_kraken = @"https://min-api.cryptocompare.com/data/histominute?fsym=ETH&tsym=EUR&limit=2000&aggregate=15&e=Kraken&extraParams=yeahnoworries";
var client_kraken = new WebClient();
var content_kraken = client_kraken.DownloadString(url_kraken);
var results_kraken = JsonConvert.DeserializeObject<List<CoinMarketCap_API_JSON>>(content_kraken);

2 个答案:

答案 0 :(得分:1)

只需使用Json.Net:

https://www.nuget.org/packages/Newtonsoft.Json/

要使用nuget安装它,请在程序包控制台中使用以下命令:

Install-Package Newtonsoft.Json

如果你有一个json可以映射到的特定类(例如JsonMapClass),你可以像以下一样使用它:

JsonMapClass json = JsonConvert.DeserializeObject<JsonMapClass>(jsonstring);

否则你可以使用动态对象:

dynamic jsonjson = JsonConvert.DeserializeObject(jsonstring);

答案 1 :(得分:1)

创建两个对象。例如:

First:
public class JsonData {
public string time {get; set;}
public string high {get; set;}
etc...
}

Second:
public class DataRoot {
public string Response {get; set:}
public string Type {get; set:}
public bool Aggregated{get; set:}
public List<JsonData> Data{get; set:}
}

var root = JsonConvert.DeserializeObject<DataRoot>(YOUR JSON STRING HERE);
var rootData = root.Data;

完成...