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">
//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) {
})
.error(function (response) {
alert(response.d);
});
}
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();
DnDCharacter attrToSave = ser.Deserialize<DnDCharacter>(data);
Default NewChar = new Default();
NewChar.SaveNewChar(attrToSave);
}
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);
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; }
}