如何反序列化包含多维数组的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#代码;

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

}

回答:

我建议您使用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);

–多维数组支持,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

以上是 如何反序列化包含多维数组的json对象? 的全部内容, 来源链接: utcz.com/qa/409700.html

回到顶部