用C#反序列化Kraken JSON

时间:2018-03-08 19:10:07

标签: c# json kraken.com

我曾经从Cryptocompare的API加载数据。 这篇文章让我得到了我想要的Cryptocompare。

Parsing Cryptocompare API Json data in C#

但是,我注意到这个API不是防弹的,并不总是回答答案。所以我想用Kraken本身。

现在我正在尝试反序列化新的JSON,但这并不像Cryptocompare的JSON那样简单。

以下是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}}

我无法对此JSON进行反序列化。

解决!!!

最终解决方案:

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

谢谢!我会将网址加入书签。

1 个答案:

答案 0 :(得分:1)

使用此模型对数据进行反序列化

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 Generator生成的模型

然后使用NewtonSoft对c#进行反序列化数据

E.g。

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

最终解决方案:

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