1

我从服务器收到如下 JSON:

{"0":{"0":"image1.jpg","1":"texthere","2":"0"},"1":{"0":"image66.jpg","1":"texthere","2":"1"},"2":{"0":"image12.jpg","1":"texthere","2":"2"},"3":{"0":"image44.jpg","1":"texthere","2":"3"},"4":{"0":"image34.jpg","1":"texthere","2":"4"},"5":{"0":"image33.jpg","1":"texthere","2":"5"},"6":{"0":"image21.jpg","1":"texthere","2":"6"},"7":{"0":"image32.jpg","1":"texthere","2":"7"},"8":{"0":"image13.jpg","1":"texthere","2":"8"},"9":{"0":"image11.jpg","1":"texthere","2":"9"},"10":{"0":"image03.jpg","1":"texthere","2":"10"},"length":"12"}

编写此代码的开发人员使用JSON_FORCE_OBJECT作为 PHP 中 json_encode 方法的参数。

在 JavaScript 中是否有任何“魔法”(即,不是自定义函数)将此结构转换为多维数组?

我想要类似的东西:

[["image1.jpg","texthere","2"],["image66.jpg","texthere","1"]]...

免责声明: - 我正在寻找本机实现(不是 JQuery);- PHP 最终可以更改(如果需要);

任何帮助表示赞赏,在此先感谢。

4

3 回答 3

0

不是魔术,但您可以遍历数据并测试它具有什么类型的值。

一个基本的例子如下。不过,它没有我想要在生产代码中进行的错误检查。

var data = {"0":{"0":"image1.jpg","1":"texthere","2":"0"},"1":{"0":"image66.jpg","1":"texthere","2":"1"},"2":{"0":"image12.jpg","1":"texthere","2":"2"},"3":{"0":"image44.jpg","1":"texthere","2":"3"},"4":{"0":"image34.jpg","1":"texthere","2":"4"},"5":{"0":"image33.jpg","1":"texthere","2":"5"},"6":{"0":"image21.jpg","1":"texthere","2":"6"},"7":{"0":"image32.jpg","1":"texthere","2":"7"},"8":{"0":"image13.jpg","1":"texthere","2":"8"},"9":{"0":"image11.jpg","1":"texthere","2":"9"},"10":{"0":"image03.jpg","1":"texthere","2":"10"},"length":"12"};

function data_to_array(data) {
    var array = [];
    for (var key in data) {
        var value = data[key];
        if (typeof value === 'string') {
            array[key] = value;
        } else {
            array[key] = data_to_array(value);
        }
    }
    return array;
}

var array = data_to_array(data);
console.log(array);

hasOwnProperty如果您的对象原型可能被弄乱,请确保添加检查。您可能还应该添加一个检查以确保仅将整数键添加到数组中。

于 2012-04-12T10:21:24.367 回答
0

我能想到的最简单的方法是使用正则表达式将 JSON 从对象文字转换为数组文字。

不幸的是,西蒙考威尔比这种方法更神奇。

//I don't know why you don't want a custom function.
function dataToArray(data)
{
    data = data.replace(/"[0-9]+":/g,"");          //Remove all index keys
    data = data.replace(/,"length":"[0-9]+"/g,""); //Remove length key-value pair
    data = data.replace(/{/g,"[");                 //Change the left brackets
    data = data.replace(/}/g,"]");                 //Change the right brackets

    return JSON.parse(data);
}
于 2012-04-12T10:29:47.590 回答
0

没有内置函数。如果你有 JSON 字符串,你可以做字符串替换,否则你必须循环如下所示。

var dataObject = {"0":{"0":"image1.jpg","1":"texthere","2":"0"},"1":{"0":"image66.jpg","1":"texthere","2":"1"},"2":{"0":"image12.jpg","1":"texthere","2":"2"},"3":{"0":"image44.jpg","1":"texthere","2":"3"},"4":{"0":"image34.jpg","1":"texthere","2":"4"},"5":{"0":"image33.jpg","1":"texthere","2":"5"},"6":{"0":"image21.jpg","1":"texthere","2":"6"},"7":{"0":"image32.jpg","1":"texthere","2":"7"},"8":{"0":"image13.jpg","1":"texthere","2":"8"},"9":{"0":"image11.jpg","1":"texthere","2":"9"},"10":{"0":"image03.jpg","1":"texthere","2":"10"},"length":"12"};

function getArray(object){
     var array = [];
     for(var key in object){
        var item = object[key];
        array[parseInt(key)] = (typeof(item) == "object")?getArray(item):item;
     }
     return array;
}

var dataArray = getArray(dataObject);
于 2012-04-12T10:53:48.980 回答