Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

c# - How to deseralize json object that contains multidimensional array?

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 null. Please help.

The script code;

var allFields = new Array();
allFields.push({
                        BirinciKatilimciId: birinciKatilimciId.val(),
                        IkinciKatilimciId: ikinciKatilimciId.val(),
                        BirincininSkoru: birincininSkoru.val(),
                        IkincininSkoru: ikincininSkoru.val(),
                        MacSayisi: macSayisi.val(),
                        MacSuresi: macinSuresi.val(),
                        MacinOynanmaSaati: macinOynanmaSaati.val(),
                        Hukmen: hukmen.is(':checked'),
                        RatingeDahil: ratingeDahil.is(':checked'),
                        MaclarTablosundaGoster: maclarTablosundaGoster.is(':checked'),
                        MacinTarihi: macinTarihi.val()
                    });

$("#<%=btnMaclariKaydet.ClientID %>").click(function () {

            var jsonText = JSON.stringify({
                arrayAllMaclar: allFields
            });
            $('#<%= hfMaclar.ClientID %>').val(jsonText);

        });

c# code;

protected void btnMaclariKaydet_Click(object sender, EventArgs e)
    {
        JavaScriptSerializer ser = new JavaScriptSerializer();
        JsonMaclar turnuvaMaclar = ser.Deserialize<JsonMaclar>(hfMaclar.Value);
    }
public class JsonMaclar {
    List<JsonMac> arrayAllMaclar { get; set; }    
}

public class JsonMac {
    String BirinciKatilimciId { get; set; }
    String IkinciKatilimciId { get; set; }
    String BirincininSkoru { get; set; }
    String IkincininSkoru { get; set; }
    String MacSayisi { get; set; }
    String MacSuresi { get; set; }
    String MacinOynanmaSaati { get; set; }
    String Hukmen { get; set; }
    String RatingeDahil { get; set; }
    String MaclarTablosundaGoster { get; set; }
    String MacinTarihi { get; set; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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<Product>(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<string[,]>(json);

Console.WriteLine(deserialized[3, 0] + ", " + deserialized[3, 1]);
// Han, Leia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...