将JSON数据转换为二维数组

时间:2020-12-08 21:37:23

JSON Data sample:

JSON数据示例:

 { 
        "GroupA": "Array[37]", 
        "AGroupC": "Array[9]", 
        "GroupB": "Array[12]", 
        "GroupE": "Array[15]", 
        "GroupF": "Array[21]", 
        "GroupD": "Array[6]" 
        }

I have the above data and I need to convert it to two dimensional array but don't know how to do it. Any help will be appreciated. The resulting should look like:

我有上面的数据,我需要把它转换成二维数组但不知道怎么做。如有任何帮助,我们将不胜感激。结果应该是:

["GroupA", "GroupC", "AGroupB", "GroupE", "GroupF", "GroupD"]
[37, 9, 12, 15, 21, 6]

Thanks.

谢谢。

3 个解决方案

#1


1  

Try this:

试试这个:

var dataJson = {
  "GroupA": "Array[37]",
  "AGroupC": "Array[9]",
  "GroupB": "Array[12]",
  "GroupE": "Array[15]",
  "GroupF": "Array[21]",
  "GroupD": "Array[6]"
};

var groupArr = [];
var arrayArr = [];

for (var i in dataJson) {
  if (dataJson.hasOwnProperty(i)) {
    groupArr.push(i);
    arrayArr.push(dataJson[i].match(/\d+/)[0]);
  }
}
alert('groupArr: ' + JSON.stringify(groupArr));
alert('arrayArr: ' + JSON.stringify(arrayArr));

#2


0  

From what I can tell, this is what you are asking for.

据我所知,这就是你所要求的。

function convertJSONto2D(json) {
    var 2dArray = [];
    var result  = [];

    2dArray.push(Object.keys(json));

    for(key in json) {
        if (object.hasOwnProperty(key)) {
            result.push(json[key].length);
        }
    }

    2dArray.push(result);

    return 2dArray;
}

#3


0  

Try sometihing like this:

尝试sometihing是这样的:

    var dataJson = { 
            "GroupA": "Array[37]", 
            "AGroupC": "Array[9]", 
            "GroupB": "Array[12]", 
            "GroupE": "Array[15]", 
            "GroupF": "Array[21]", 
            "GroupD": "Array[6]" 
            }


    var groupArr = [];
    var arrayArr = [];

    function dataArr() {

        for (i = 0; i < (dataJson.length); i++) {
            groupArr.push(dataJson[i+i]);
            arrayArr.push(dataJson[i+1]);
        }   
    }

//output of each:
//groupArr
// ["GroupA", "GroupC", "AGroupB", "GroupE", "GroupF", "GroupD"]
//arrayArr
// ["Array[37]", "Array[9]", "Array[12]", "Array[15]", "Array[21]", "Array[6]"]

#1


1  

Try this:

试试这个:

var dataJson = {
  "GroupA": "Array[37]",
  "AGroupC": "Array[9]",
  "GroupB": "Array[12]",
  "GroupE": "Array[15]",
  "GroupF": "Array[21]",
  "GroupD": "Array[6]"
};

var groupArr = [];
var arrayArr = [];

for (var i in dataJson) {
  if (dataJson.hasOwnProperty(i)) {
    groupArr.push(i);
    arrayArr.push(dataJson[i].match(/\d+/)[0]);
  }
}
alert('groupArr: ' + JSON.stringify(groupArr));
alert('arrayArr: ' + JSON.stringify(arrayArr));

#2


0  

From what I can tell, this is what you are asking for.

据我所知,这就是你所要求的。

function convertJSONto2D(json) {
    var 2dArray = [];
    var result  = [];

    2dArray.push(Object.keys(json));

    for(key in json) {
        if (object.hasOwnProperty(key)) {
            result.push(json[key].length);
        }
    }

    2dArray.push(result);

    return 2dArray;
}

#3


0  

Try sometihing like this:

尝试sometihing是这样的:

    var dataJson = { 
            "GroupA": "Array[37]", 
            "AGroupC": "Array[9]", 
            "GroupB": "Array[12]", 
            "GroupE": "Array[15]", 
            "GroupF": "Array[21]", 
            "GroupD": "Array[6]" 
            }


    var groupArr = [];
    var arrayArr = [];

    function dataArr() {

        for (i = 0; i < (dataJson.length); i++) {
            groupArr.push(dataJson[i+i]);
            arrayArr.push(dataJson[i+1]);
        }   
    }

//output of each:
//groupArr
// ["GroupA", "GroupC", "AGroupB", "GroupE", "GroupF", "GroupD"]
//arrayArr
// ["Array[37]", "Array[9]", "Array[12]", "Array[15]", "Array[21]", "Array[6]"]