Click here to Skip to main content
15,887,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Ive researched this as much as I could. Im getting a a 2 dimensional array from javascript from a json string.
JavaScript
<pre lang="Javascript">

//Create 2 demensional arrays
C#
//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);
            });
}



Above is how I create my 2D array and pass it to my webmethod in the codebehind.

This is my method.

C#
<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);
     }



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.

C#
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!
C#
<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; }
        }


Posted

1 solution

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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900