JSON 嵌套数组。 C# DataContractJsonSerializer returns 空

JSON nested array. C# DataContractJsonSerializer returns null

我明白了 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"]]}

我使用 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; }
}

我得到 LastUpdateId 值,我得到数组 Asks 和数组 Bids,每个包含 10 个元素。但是嵌套在其中的值数组的值为空。如何获取这些数据? 谢谢

我把你的代码放到了一个小控制台应用程序中。我让它像这样工作:

    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; }

    }

就个人而言,我非常喜欢使用 Lists<>'s 而不是 Array's。它们提供了更多的灵活性和功能,并且更易于使用。