0

I'm trying to deserialize some Json data to a list, however the list returns no data.

An example of the Json string I am using is

{
"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]
]

}

I used json2csharp to generate the object class which gave me

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

and this is my method I'm using to deserialize the 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);            
    }

I am getting valid Json returned in the response string, not sure why the productOrderBooks list has no data. Do I have to create two more object classes to hold the bid and ask data and pass these into

 public List<List<object>> bids { get; set; }
 public List<List<object>> asks { get; set; }
3
  • It looks like your json is a single product order book but you are trying to deseralize an array of them is this the case?
    – Sam Marion
    Commented Nov 19, 2017 at 3:13
  • Is that your full JSON, or is there an outer array or a wrapper object? Commented Nov 19, 2017 at 3:14
  • Hi Sam and Brian, thanks for taking the time to help. Yeah thats the full Json no wrapper. Thanks sam for pointing that out to me got a bit confused there for a moment with there being Lists in the object class given by Json2CSharp, all fixed now thanks. Commented Nov 19, 2017 at 3:34

1 Answer 1

2

As per your posted json, it is not list of ProductOrderBook. It's just single instance of ProductOrderBook.

So, your code should be :

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);            
    }
2
  • Hi Akash thanks pointing this out, dont why I was being so Silly, this fix worked, Many Thanks. Commented Nov 19, 2017 at 3:35
  • Glad, it helped.
    – Akash KC
    Commented Nov 19, 2017 at 3:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.