反序列化Json对象列表返回null

时间:2017-11-19 03:10:57

标签: c# json deserialization

我正在尝试将一些Json数据反序列化到列表中,但是列表不返回任何数据。

我正在使用的Json字符串的一个例子是

{
"sequence":82334,
"bids":
[
    ["7660","10.02477743",11],
    ["7600","0.01",1],
    ["7500","0.01",1]
],  
"asks":
[
    ["7672.57","0.63979186",1],
    ["7673.64","1",1],
    ["7678.95","1",1]
]

}

我用json2csharp生成了给我的对象类

public class ProductOrderBook
{
   public int sequence { get; set; }
   public List<List<object>> bids { get; set; }
   public List<List<object>> asks { get; set; }
}

这是我用来反序列化json的方法

public static async Task<List<ProductOrderBook>> GetProductOrderBook()
{
    string ts = GetNonce();
    string method = "/products/BTC-USD/book?level=2";
    string sig = GetSignature(ts, "GET", method, string.Empty);
    List<ProductOrderBook> productOrderBooks;
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(baseURL);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("CB-ACCESS-KEY", apiKey);
        client.DefaultRequestHeaders.Add("CB-ACCESS-SIGN", sig);
        client.DefaultRequestHeaders.Add("CB-ACCESS-TIMESTAMP", ts);
        client.DefaultRequestHeaders.Add("CB-ACCESS-PASSPHRASE", passphrase);
        client.DefaultRequestHeaders.Add("User-Agent", userAgent);

        HttpResponseMessage response = client.GetAsync(method).Result;
        string json = await response.Content.ReadAsStringAsync();
        productOrderBooks = JsonConvert.DeserializeObject<List<ProductOrderBook>>(json);
        }
        return await Task.Run(() => productOrderBooks);            
    }

我在响应字符串中返回了有效的Json,不知道为什么productOrderBooks列表没有数据。我是否必须创建另外两个对象类来保存出价并询问数据并将其传递到

 public List<List<object>> bids { get; set; }
 public List<List<object>> asks { get; set; }

1 个答案:

答案 0 :(得分:2)

根据您发布的json,它不是ProductOrderBook的列表。它只是ProductOrderBook的单个实例。

所以,你的代码应该是:

public static async Task<ProductOrderBook> GetProductOrderBook()
{
    string ts = GetNonce();
    string method = "/products/BTC-USD/book?level=2";
    string sig = GetSignature(ts, "GET", method, string.Empty);
    ProductOrderBook productOrderBooks;
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(baseURL);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Add("CB-ACCESS-KEY", apiKey);
        client.DefaultRequestHeaders.Add("CB-ACCESS-SIGN", sig);
        client.DefaultRequestHeaders.Add("CB-ACCESS-TIMESTAMP", ts);
        client.DefaultRequestHeaders.Add("CB-ACCESS-PASSPHRASE", passphrase);
        client.DefaultRequestHeaders.Add("User-Agent", userAgent);

        HttpResponseMessage response = client.GetAsync(method).Result;
        string json = await response.Content.ReadAsStringAsync();
        productOrderBooks = JsonConvert.DeserializeObject<ProductOrderBook>(json);
        }
        return await Task.Run(() =>  productOrderBooks);            
    }