如何在C#/ Javascript中反序列化二维数组 [英] How to Deserialize a 2 dimensional array in C#/Javascript
问题描述
大家好,
我已经尽可能多地研究了这个。我从json字符串中获取javascript的二维数组。
Hi all,
Ive researched this as much as I could. Im getting a a 2 dimensional array from javascript from a json string.
<pre lang="Javascript">
//创建2个版本的阵列
//Create 2 demensional arrays
//Create 2 demensional arrays
var iMax = 2;
var jMax = 6;
var AttrTree = new Array();
for (i = 0; i < iMax; i++) {
AttrTree[i] = new Array();
for (j = 0; j < jMax; j++) {
AttrTree[i][j] = 0;
}
}
for (var i = 0; i < 6; i++) {
AttrTree[0][i] = document.getElementById("ContentPlaceHolder1_dlAttributes_txtAttrBase_" + i).value;
AttrTree[1][i] = document.getElementById("ContentPlaceHolder1_dlAttributes_txtTempAttrBase_" + i).value;
}
var json = {
Attr: AttrTree };
var loc = window.location.href;
$.ajax({
type: 'POST',
url: loc + '/AttrScoreChanged',
data: "{'data':'" + JSON.stringify(json) + "'}",
dataType: 'json',
contentType: "application/json; charset=utf-8"
})
.success(function (response) {
//alert(response.d);
})
.error(function (response) {
alert(response.d);
});
}
以上是我如何创建我的2D数组并将其传递给我的webmethod在代码隐藏中。
这是我的方法。
Above is how I create my 2D array and pass it to my webmethod in the codebehind.
This is my method.
<pre lang="xml">public static void AttrScoreChanged(string data)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
//List<DnDCharacter> wrapper = ser.Deserialize<List<DnDCharacter>>(data);
DnDCharacter attrToSave = ser.Deserialize<DnDCharacter>(data);
//var attrToSave = ser.Deserialize<dynamic>(data);
Default NewChar = new Default();
NewChar.SaveNewChar(attrToSave);
}
现在,如果我使用var attrToSave = ser.Deserialize<,它的工作正常;动态>(数据);
但是,我不能用它做很多事情。我无法从中提取值。
Now, it works ok if I use var attrToSave = ser.Deserialize<dynamic>(data);
But, I cant really do much with it. I cant extract values from it.
List<DnDCharacter> wrapper = ser.Deserialize<List<DnDCharacter>>(data);
它没有给我任何错误,但它实际上没有用。包装器中没有任何内容。
DnDCharacter attrToSave = ser.Deserialize< dndcharacter>(数据);
这是首选方式我喜欢这样做。因为这个json将包含2d数组和单个字符串。但是,当我这样做时,我收到一个错误:这不支持反序列化数组。我已经安装了json.net dll,这是为了纠正这个问题。
所以,简而言之,我输了。 :D非常感谢帮助!
It doesnt give me any error but it doesnt actually work. The wrapper will have nothing in it.
DnDCharacter attrToSave = ser.Deserialize<dndcharacter>(data);
This is the preferred way Id like to do it. Since this json will contain both 2d arrays and single strings. But, when I do this, I get an error: this not supported for deserialization of an array. Ive installed the json.net dll that is suppose to correct this.
So, in short, Im lost. :D Help is very appreciated!
<pre lang="cs">public class DnDCharacter
{
#region Attributes
string[,] Attributes = new string[6, 2];
#endregion
#region Properties
public string[,] Attr
{
get { return Attributes; }
set { Attributes = value; }
}
推荐答案
.ajax({
type:' POST',
url:loc + ' / AttrScoreChanged',
data: {'data':' + JSON.stringify(json)+ '},
dataType:' json',
contentType: application / json; charset = utf-8
} )
.success(函数(响应){
// alert(response.d);
})
.error(函数(响应){
alert(res) ponse.d);
});
}
.ajax({ type: 'POST', url: loc + '/AttrScoreChanged', data: "{'data':'" + JSON.stringify(json) + "'}", dataType: 'json', contentType: "application/json; charset=utf-8" }) .success(function (response) { //alert(response.d); }) .error(function (response) { alert(response.d); }); }
以上是我如何创建我的2D数组并将其传递给我的webmethod在代码隐藏中。
这是我的方法。
Above is how I create my 2D array and pass it to my webmethod in the codebehind.
This is my method.
<pre lang="xml">public static void AttrScoreChanged(string data)
{
JavaScriptSerializer ser = new JavaScriptSerializer();
//List<DnDCharacter> wrapper = ser.Deserialize<List<DnDCharacter>>(data);
DnDCharacter attrToSave = ser.Deserialize<DnDCharacter>(data);
//var attrToSave = ser.Deserialize<dynamic>(data);
Default NewChar = new Default();
NewChar.SaveNewChar(attrToSave);
}
现在,如果我使用var attrToSave = ser.Deserialize<,它的工作正常;动态>(数据);
但是,我不能用它做很多事情。我无法从中提取值。
Now, it works ok if I use var attrToSave = ser.Deserialize<dynamic>(data);
But, I cant really do much with it. I cant extract values from it.
List<DnDCharacter> wrapper = ser.Deserialize<List<DnDCharacter>>(data);
它没有给我任何错误,但它实际上没有用。包装器中没有任何内容。
DnDCharacter attrToSave = ser.Deserialize< dndcharacter>(数据);
这是首选方式我喜欢这样做。因为这个json将包含2d数组和单个字符串。但是,当我这样做时,我收到一个错误:这不支持反序列化数组。我已经安装了json.net dll,这是为了纠正这个问题。
所以,简而言之,我输了。 :D非常感谢帮助!
It doesnt give me any error but it doesnt actually work. The wrapper will have nothing in it.
DnDCharacter attrToSave = ser.Deserialize<dndcharacter>(data);
This is the preferred way Id like to do it. Since this json will contain both 2d arrays and single strings. But, when I do this, I get an error: this not supported for deserialization of an array. Ive installed the json.net dll that is suppose to correct this.
So, in short, Im lost. :D Help is very appreciated!
<pre lang="cs">public class DnDCharacter
{
#region Attributes
string[,] Attributes = new string[6, 2];
#endregion
#region Properties
public string[,] Attr
{
get { return Attributes; }
set { Attributes = value; }
}
JSON中的2D数组:[[1,2],[3,4]]
首先检查JSON字符串是否与上面完全相同。
在你的情况下整个字符串就像:{'Attr':[[1,2],[3,4]]}
单个实例
现在如果你想发送那个类型的列表,那么JSON就像: [{'Attr':[[1,2],[3,4]]},{'Attr':[[10,20],[31,40]]}] 大>
现在这个字符串可以转换为List< dndcharacter>
Hi,
2D array in JSON: [[1,2],[3,4]]
First Check the JSON string is that showing exactly like above.
in your case the whole string would be like : { 'Attr' : [[1,2],[3,4]] }
for single instance
Now if you want to send a list of that type then JSON would be like : [{ 'Attr' : [[1,2],[3,4]] },{ 'Attr' : [[10,20],[31,40]] }]
Now this string you can cast into a List<dndcharacter>
这篇关于如何在C#/ Javascript中反序列化二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!