如何将javascript中的嵌套数组转换为json结构

时间:2014-04-30 01:49:33

标签: javascript json

在下面的代码中我试图填充javascript类,这样我得到一个可以字符串化为json字符串的对象。             

       function classValue(valuearrs) {
        for (var i = 0; i < valuearrs.length; i++) {
            for (j = 0; j < valuearrs[i].length; j++) {

                this.id = valuearrs[i][j];
                this.name = valuearrs[i][j];
                this.somekey = valuearrs[i][j];
            }
        }
  }


   function CreatenestedarrObj( valuearr) {

        this.Arrayparameters = [new classValue (valuearr)];
    }

   function ArrayMethodCall() {

       var valuearr = new Array();
       valuearr[0] = "myval1";
       valuearr[1] = "myval2";
       valuearr[2] = "myval3";
       var valuearr1 = new Array();
       valuearr1[0] = "myval11";
       valuearr1[1] = "myval12";
       valuearr1[2] = "myval13";
       nestedarr = new Array();
       nestedarr[0] = valuearr;
       nestedarr[1] = valuearr1;

       var x = new CreatenestedarrObj( nestedarr);
       var strobject = JSON.stringify(x);
       alert(strobject);
   }

   </script>
  </head> <body> MethodCall: <input type="button" value="Call Method" onclick=" ArrayMethodCall ()" />

在字符串化后,预期的值是{ArrayParameters:[{myval1,myval2,myval3},{myval1,myval12,myval13}]}。我能想到的事情是我每次都在循环中创建新对象但是如何?或者我可能完全错了。非常感谢任何帮助。

3 个答案:

答案 0 :(得分:4)

试试这个

    function classValue(valuearrs) {
        var arr = []
        for (var i = 0; i < valuearrs.length; i++) {
            for (j = 0; j < valuearrs[i].length; j++) {
                var temp = new Object();
                temp.id = valuearrs[i][j];
                temp.name = valuearrs[i][j];
                temp.somekey = valuearrs[i][j];
                arr[j] = temp;
            }
        }
        return arr;
  }

答案 1 :(得分:2)

改变这个:

function classValue(valuearrs) {
        var arr=[]
          for (var i = 0; i < valuearrs.length; i++) {
              arr[i]={};
            for (j = 0; j < valuearrs[i].length; j++) {

                arr[i].id = valuearrs[i][j];
                arr[i].name = valuearrs[i][j];
                arr[i].somekey = valuearrs[i][j];
            }
        }
          return arr;
  }

答案 2 :(得分:0)

我不知道为什么你需要将nestedarr分为idnamesomekey 但如果你不需要,只需返回valuearrs即可,你会没事的:

function classValue(valuearrs) {
    return valuearrs;
  }


   function CreatenestedarrObj( valuearr) {

        this.Arrayparameters = [new classValue (valuearr)];
    }

   function ArrayMethodCall() {

       var valuearr = new Array();
       valuearr[0] = "myval1";
       valuearr[1] = "myval2";
       valuearr[2] = "myval3";
       var valuearr1 = new Array();
       valuearr1[0] = "myval11";
       valuearr1[1] = "myval12";
       valuearr1[2] = "myval13";
       nestedarr = new Array();
       nestedarr[0] = valuearr;
       nestedarr[1] = valuearr1;

       var x = new CreatenestedarrObj( nestedarr);
       var strobject = JSON.stringify(x);
       console.info(strobject);
   }

输出

{"Arrayparameters":[[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]]}  

或者如果你不希望它Arrayparameters好得多,你可以使用以下代码

var strobject = JSON.stringify(nestedarr);

,输出将如下:

[["myval1","myval2","myval3"],["myval11","myval12","myval13"]]