1

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.

2
  • how about trying to change double[] to list of double -> List<double> Commented Oct 22, 2019 at 17:36
  • Thanks, Ivan-San. It works: public List<List<double>> Bids { get; set; } Commented Oct 23, 2019 at 13:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Роман, glad to hear it! Would you mind marking my answer as correct? Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.