首页 > 解决方案 > C# - 无法反序列化当前 JSON 对象

问题描述

我知道有很多关于这个的线程,但我不明白这个 json 输出。我之前已经设法从这个站点反序列化 json,但它们不同。

我的课看起来像这样:

public class Bids
{
    public string bids { get; set; }
    public string asks { get; set; }
    public string lastUpdateId { get; set; }
}

下载数据:

var depth = w.DownloadString("https://api.binance.com/api/v1/depth?symbol=BTCUSDT&limit=20");

反序列化:

var book = JsonConvert.DeserializeObject<Bids[]>(depth);

“无法反序列化当前 JSON 对象”

数据(https://api.binance.com/api/v1/depth?symbol=BTCUSDT&limit=20)如下所示:

{"lastUpdateId":192569803,"bids":[["6525.40000000","0.62166200",[]],["6524.40000000","0.04081700",[]],["6524.39000000","2.53414400",[]],["6524.13000000","0.53788600",[]],["6523.84000000","3.00000000",[]]],"asks":[["6527.00000000","1.54106400",[]],["6527.51000000","0.37739500",[]],["6527.53000000","0.31064700",[]],["6528.61000000","0.15400000",[]]]}

标签: c#json

解决方案


您可以通过使用出色的Json2Csharp之类的东西来了解您的课程应该是什么样子,您还可以使用 VS paste special 'paste as json..'

这是该类的外观。不要忘记更新列表!

public class RootObject
{
  public int lastUpdateId { get; set; }
  public List<List<object>> bids { get; set; }
  public List<List<object>> asks { get; set; }
}

推荐阅读