Deserialise a Kraken JSON in C#

坚强是说给别人听的谎言 提交于 2019-12-13 08:35:37

问题


I used to be loading data from Cryptocompare's API. This post got me what I wanted for Cryptocompare.

Parsing Cryptocompare API Json data in C#

However, I notice that this API is not bulletproof, it's not always returning an answer. So I want to use Kraken itself.

Now I am trying to deserialise the new JSON but this is not straightforward as Cryptocompare's JSON.

Here is an example JSON:

https://api.kraken.com/0/public/OHLC?pair=ETHUSD&interval=30

{"error":[],"result":{"XETHZUSD":[[1519236000,"825.94","846.00","825.94","845.00","835.62","858.29381033",708],[1519237800,"846.00","848.84","835.55","836.00","844.31","647.42747317",731],[1519239600,"836.00","841.09","830.76","834.89","835.13","1051.44905097",609],[1520530200,"706.24","710.43","701.90","704.59","707.70","1763.41692283",459]],"last":1520528400}}

I have not been able to deserialise this JSON.

Solved!!!

final solution:

    var Results_Exchange_Kraken = JsonConvert.DeserializeObject<DataRoot_Kraken>(Content_Exchange_Kraken);


    var price_data_kraken = Results_Exchange_Kraken.result.XETHZUSD;


    public class Kraken_Result
    {
        public List<List<object>> XETHZUSD { get; set; }
        public int last { get; set; }
    }

    public class DataRoot_Kraken
    {
        public List<object> error { get; set; }
        public Kraken_Result result { get; set; }
    }

Thank you! I'll bookmark the URL.


回答1:


Use this Model to de-serialise your data

public class Result
{
    public List<List<object>> XETHZUSD { get; set; }
    public int last { get; set; }
}

public class RootObject
{
    public List<object> error { get; set; }
    public Result result { get; set; }
}

Model Generated by Model Generator

Then use NewtonSoft for c# to de-serialise your data

E.g.

RootObject tmp = JsonConvert.DeserializeObject<RootObject>("JSON String here");

final solution:

    var Results_Exchange_Kraken = JsonConvert.DeserializeObject<DataRoot_Kraken>(Content_Exchange_Kraken);


    var price_data_kraken = Results_Exchange_Kraken.result.XETHZUSD;


    public class Kraken_Result
    {
        public List<List<object>> XETHZUSD { get; set; }
        public int last { get; set; }
    }

    public class DataRoot_Kraken
    {
        public List<object> error { get; set; }
        public Kraken_Result result { get; set; }
    }


来源:https://stackoverflow.com/questions/49180714/deserialise-a-kraken-json-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!