javascript - jquery 使用JSON.stringify转 json,一直为空值
本文介绍了javascript - 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);
这篇关于javascript - jquery 使用JSON.stringify转 json,一直为空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文