简体   繁体   English

将二维javascript数组转换为JSON

[英]converting a two dimensional javascript array to JSON

i've tried a few different json methods (stringify, toJSON, and probably some totally irrelevant others out of desperation) but can't seem to figure out how to stringify this so i can pass it to a php script. 我已经尝试了几种不同的json方法(stringify,toJSON,以及出于绝望而可能有些完全不相关的其他方法),但似乎无法弄清楚如何对此进行字符串化,因此我可以将其传递给php脚本。 i am able to create a two dimensional array that which could be represented something like this: 我能够创建一个二维数组,该数组可以表示如下:

array(
    'image'=>array(
        0=>'hello.jpeg', 
        1=>'goodbye.jpeg', 
        2=>'etc.jpeg'),
    'resume'=>array(
        0=>'resume.doc'),
    'reel'=>array(
        0=>'reel.mov')
    )

the array looks okay when i print it to console using this dump function. 当我使用此转储功能将其打印到控制台时,该阵列看起来还可以。 i tried figuring out how to get this to work with objects because i thought i read something that said objects were already JSON friendly, but since i'm not super familiar with javascript i was pretty much floundering about. 我试图弄清楚如何使它与对象一起使用,因为我以为我读到一些东西说对象已经是JSON友好的了,但是由于我对javascript不太熟悉,因此我几乎陷入困境。

edit: some more details... the declaration of my array (when i had it working) was something like this, although i may have messed up: 编辑:更多详细信息...我的数组的声明(当我有它的工作时)是这样的,尽管我可能搞砸了:

var fileArray = [];
fileArray['image'] = [];
fileArray['resume'] = [];
fileArray['reel'] = [];

var type;
var name;

var divs = $("#upload-container").children('div');

$.each(divs, function() {
    type = $(this).attr('id');
    name = $(this).html();

    fileArray[type].push(name);
});

The object for that array structure might look like this in JavaScript: 该数组结构的对象在JavaScript中可能如下所示:

var objects = 
[
    {
        'image': [
            { '0': 'hello.jpeg' },
            { '1': 'goodbye.jpeg' },
            { '2': 'etc.jpeg' }
        ]
    },
    {
        'resume': [
            { '0': 'resume.doc' }
        ]
    },
    {
        'reel': [
            { '0': 'reel.mov' }
        ]
    }
]

So now you've got an array of three objects, each of which contains a property (image, resume, reel) that is another array of objects with basically key:value pairs ( '0':'hello.jpeg' ). 因此,现在您有了一个包含三个对象的数组,每个对象包含一个属性(图像,履历,卷轴),该属性是另一个对象数组,这些对象基本上具有key:value对( '0':'hello.jpeg' )。 Maybe you could simplify it by not bothering to use the indexes: 也许您可以通过不使用索引来简化它:

var objects = 
[
    {
        'image': [ 'hello.jpeg', 'goodbye.jpeg', 'etc.jpeg' ]
    },
    {
        'resume': [ 'resume.doc' ],
    },
    {
        'reel': [ 'reel.mov' ]
    }
]

Then you can use JSON.stringify(objects) to pass to your PHP action. 然后,您可以使用JSON.stringify(objects)传递给您的PHP操作。

Your sample expected output on the PHP side has an associative array containing numeric arrays. PHP方面的示例预期输出具有包含数字数组的关联数组。 JavaScript arrays have numeric indexes: if you want strings as keys use a plain object rather than an array because JavaScript objects act like the associative arrays you are thinking of from PHP. JavaScript数组具有数字索引:如果要将字符串用作键,请使用普通对象而不是数组,因为JavaScript对象的作用类似于您从PHP想到的关联数组。 The corresponding JavaScript object should look like this: 相应的JavaScript对象应如下所示:

var fileArray = {
  'image' : [ 'hello.jpeg',
             'goodbye.jpeg',
             'etc.jpeg'],
  'resume' : [ 'resume.doc' ],
  'reel' : [ 'reel.mov' ]
};

In the arrays defined with square brackets the numeric indexes are implied. 在用方括号定义的数组中,暗含数字索引。 Note that fileArray is defined with curly braces not square brackets and so is not an array (in spite of its name). 请注意, fileArray是用大括号而不是方括号定义的,因此不是数组(尽管其名称)。 This still allows you to use the fileArray['image'] = ... syntax if you want to set the properties one at a time: 如果您想一次设置一个属性,这仍然允许您使用fileArray['image'] = ...语法:

var fileArray = {}; // an object, not an array
fileArray['image'] = [];
fileArray['resume'] = [];
fileArray['reel'] = [];  

Note that the curly brackets in the initial declaration make it an object but properties are still accessed with square bracket syntax. 请注意,初始声明中的大括号使它成为一个对象,但是仍然可以使用方括号语法访问属性。

The way you were defining fileArray as a JavaScript array with square brackets still allows you to add string-based key properties because arrays are objects, but JSON stringify routines may ignore those properties and only serialise the numerically indexed properties. 您将fileArray定义为带有方括号的JavaScript数组的方式仍然允许您添加基于字符串的键属性,因为数组对象,但是JSON字符串化例程可能会忽略这些属性,而只会序列化数字索引的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM