首页 > jquery 使用JSON.stringify转 json,一直为空值

jquery 使用JSON.stringify转 json,一直为空值

var post = new Array(); 
post['class'] = '1';
post['type']  = '2';
post['id']  = '3';
var postjson = JSON.stringify(post);
console.log(postjson);

console.log(postjson)出来是一个[]
对 javascript 和 jquery 完全不熟悉,不知道问题在哪儿。

谢谢大家。


你这样的写法是new了一个数组对象,然后给数组对象设置属性,但是数组序列化的时候只会序列化数组中的元素,会忽略对象上的属性和值。
按你的需求,直接用一个空对象来存放属性就行。

var post = {}; 
post['class'] = '1';
post['type']  = '2';
post['id']  = '3';
var postjson = JSON.stringify(post);
console.log(postjson);

如果我没记错的话js的数组是不能有键名的,你得用对象

【热门文章】
【热门文章】