Reputation: 13
I get this JSON:
{"lastUpdateId":836890951,"bids":[["0.02106900","0.56000000"],["0.02106800","1.38000000"],["0.02106600","3.63600000"],["0.02106500","34.28000000"],["0.02106100","41.49400000"],["0.02105900","1.82500000"],["0.02105800","17.91500000"],["0.02105700","23.73300000"],["0.02105600","28.47900000"],["0.02105500","18.37500000"]],"asks":[["0.02107100","0.36600000"],["0.02107300","0.47400000"],["0.02107500","43.56200000"],["0.02107600","28.49700000"],["0.02107700","10.37400000"],["0.02107800","8.00000000"],["0.02107900","17.35500000"],["0.02108100","23.74100000"],["0.02108200","1.35200000"],["0.02108300","2.31900000"]]}
I use DataContractJsonSerializer:
[DataContract]
[Serializable]
public partial class OrderBooK
{
public OrderBooK(string json)
{
OrderBooK deserializedUser = new OrderBooK();
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser = new DataContractJsonSerializer(deserializedUser.GetType());
deserializedUser = ser.ReadObject(ms) as OrderBooK;
ms.Close();
}
public OrderBooK()
{
}
[DataMember(Name = "lastUpdateId")]
public long LastUpdateId { get; set; }
[DataMember(Name = "bids")]
public Bids[] Bids { get; set; }
[DataMember(Name = "asks")]
public Asks[] Asks { get; set; }
}
[DataContract]
[Serializable]
public partial class Bids
{
[DataMember(Name = " ")]
public double[] values { get; set; }
}
[DataContract]
[Serializable]
public partial class Asks
{
[DataMember(Name = " ")]
public double[] values { get; set; }
}
I get LastUpdateId value and I get array Asks and array Bids consisting of 10 elements each. But the values arrays nested in them have a value of null. How to get this data? Thanks.
Upvotes: 1
Views: 136
Reputation: 13955
I took your code and put it into a little console app. I got it to work like this:
class Program
{
static void Main(string[] args)
{
string json = @"{""lastUpdateId"":836890951,""bids"":[[""0.02106900"",""0.56000000""],[""0.02106800"",""1.38000000""],[""0.02106600"",""3.63600000""],[""0.02106500"",""34.28000000""],[""0.02106100"",""41.49400000""],[""0.02105900"",""1.82500000""],[""0.02105800"",""17.91500000""],[""0.02105700"",""23.73300000""],[""0.02105600"",""28.47900000""],[""0.02105500"",""18.37500000""]],""asks"":[[""0.02107100"",""0.36600000""],[""0.02107300"",""0.47400000""],[""0.02107500"",""43.56200000""],[""0.02107600"",""28.49700000""],[""0.02107700"",""10.37400000""],[""0.02107800"",""8.00000000""],[""0.02107900"",""17.35500000""],[""0.02108100"",""23.74100000""],[""0.02108200"",""1.35200000""],[""0.02108300"",""2.31900000""]]}";
OrderBook orderBook = Newtonsoft.Json.JsonConvert.DeserializeObject<OrderBook>(json);
}
}
[Serializable]
public partial class OrderBook
{
public long lastUpdateId { get; set; }
public List<List<double>> bids { get; set; }
public List<List<double>> asks { get; set; }
}
Personally, I really like working with Lists<>'s
instead of Array's
. They provide so much more flexibility and functionality and are easier to work with.
Upvotes: 2