How to deseralize json object that contains multidimensional array?

后端 未结 1 1601
不知归路
不知归路 2020-12-18 05:31

I need some help about converting JSON object that contains multidimensional array to my class. I tried to deserialize the json object but failed. JsonMaclar class object is

1条回答
  •  醉梦人生
    2020-12-18 06:29

    I recommend you to use JSON.NET. it is an open source library to serialize and deserialize your c# objects into json and Json objects into .net objects ...

    Serialization Example:

    Product product = new Product();
    product.Name = "Apple";
    product.Expiry = new DateTime(2008, 12, 28);
    product.Price = 3.99M;
    product.Sizes = new string[] { "Small", "Medium", "Large" };
    
    string json = JsonConvert.SerializeObject(product);
    //{
    //  "Name": "Apple",
    //  "Expiry": new Date(1230422400000),
    //  "Price": 3.99,
    //  "Sizes": [
    //    "Small",
    //    "Medium",
    //    "Large"
    //  ]
    //}
    
    Product deserializedProduct = JsonConvert.DeserializeObject(json);
    

    Json.NET 4.5 Release 8 – Multidimensional Array Support, Unicode Improvements Json.NET now supports serializing and deserializing multidimensional arrays. There isn't anything you need to do, if one of your types has a multidimensional array property It Just Works™.

    string[,] famousCouples = new string[,]
      {
        { "Adam", "Eve" },
        { "Bonnie", "Clyde" },
        { "Donald", "Daisy" },
        { "Han", "Leia" }
      };
    
    string json = JsonConvert.SerializeObject(famousCouples, Formatting.Indented);
    // [
    //   ["Adam", "Eve"],
    //   ["Bonnie", "Clyde"],
    //   ["Donald", "Daisy"],
    //   ["Han", "Leia"]
    // ]
    
    string[,] deserialized = JsonConvert.DeserializeObject(json);
    
    Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
    // Han, Leia
    

    0 讨论(0)
提交回复
热议问题