如何反序列化包含多维数组的json对象?

时间:2012-11-20 09:08:07

标签: c# javascript .net json

我需要一些关于将包含多维数组的JSON对象转换为我的类的帮助。我试图反序列化json对象但失败了。 JsonMaclar类对象为null。请帮忙。

脚本代码;

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#c​​ode;

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

1 个答案:

答案 0 :(得分:9)

我建议您使用JSON.NET。它是一个开源库,用于将c#对象序列化和反序列化为json和Json对象到.net对象...

序列化示例:

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 - 多维数组支持,Unicode改进 Json.NET现在支持序列化和反序列化多维数组。如果您的某个类型具有多维数组属性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