在 C# 中从 Kraken JSON 反序列化

Deserialise a Kraken JSON in C#

我曾经从 Cryptocompare 的 API 加载数据。 post 得到了我想要的 Cryptocompare。

但是,我注意到这个 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; }
    }

谢谢!我会为 URL.

添加书签

使用此模型de-serialise 您的数据

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 for c# 来 de-serialise 你的数据

例如

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