如何将 javascript 对象转换成 json字符串
javascript 对象转换成 json字符串[js对象转换成json字符串]
使用$.toJSON(Object)就可以转换了,转换之前先引入jquery.json.js
(function($) {
$.toJSON = function(o)
{
if (typeof(JSON) == 'object' JSON.stringify)
return JSON.stringify(o);
var type = typeof(o);
if (o === null)
return "null";
if (type == "undefined")
return undefined;
if (type == "number" || type == "boolean")
return o + "";
if (type == "string")
return $.quoteString(o);
if (type == 'object')
{
if (typeof o.toJSON == "function")
return $.toJSON( o.toJSON() );
if (o.constructor === Date)
{
var month = o.getUTCMonth() + 1;
if (month 10) month = '0' + month;
var d*** = o.getUTCDate();
if (d*** 10) d*** = '0' + d***;
var year = o.getUTCFullYear();
var hours = o.getUTCHours();
if (hours 10) hours = '0' + hours;
var minutes = o.getUTCMinutes();
if (minutes 10) minutes = '0' + minutes;
var seconds = o.getUTCSeconds();
if (seconds 10) seconds = '0' + seconds;
var milli = o.getUTCMilliseconds();
if (milli 100) milli = '0' + milli;
if (milli 10) milli = '0' + milli;
return '"' + year + '-' + month + '-' + d*** + 'T' +
hours + ':' + minutes + ':' + seconds +
'.' + milli + 'Z"';
}
if (o.constructor === Arr***)
{
var ret = [];
for (var i = 0; i o.length; i++)
ret.push( $.toJSON(o[i]) || "null" );
return "[" + ret.join(",") + "]";
}
var pairs = [];
for (var k in o) {
var name;
var type = typeof k;
if (type == "number")
name = '"' + k + '"';
else if (type == "string")
name = $.quoteString(k);
else
continue; //skip non-string or number keys
if (typeof o[k] == "function")
continue; //skip pairs where the value is a function.
var val = $.toJSON(o[k]);
pairs.push(name + ":" + val);
}
return "{" + pairs.join(", ") + "}";
}
};
$.evalJSON = function(src)
{
if (typeof(JSON) == 'object' JSON.parse)
return JSON.parse(src);
return eval("(" + src + ")");
};
$.secureevalJSON = function(src)
{
if (typeof(JSON) == 'object' JSON.parse)
return JSON.parse(src);
var filtered = src;
filtered = filtered.replace(/\["\/bfnrtu]/g, '@');
filtered = filtered.replace(/"[^"\nr]*"|true|false|null|-?d+(?:.d*)?(?:[eE][+-]?d+)?/g, ']');
filtered = filtered.replace(/(?:^|:|,)(?:s*[)+/g, '');
if (/^[],:{}s]*$/.test(filtered))
return eval("(" + src + ")");
else
throw new SyntaxError("Error parsing JSON, source is not valid.");
};
$.quoteString = function(string)
{
if (string.match(_escapeable))
{
return '"' + string.replace(_escapeable, function (a)
{
var c = _meta[a];
if (typeof c === 'string') return c;
c = a.charCodeAt();
return '\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
}) + '"';
}
return '"' + string + '"';
};
var _escapeable = /["\x00-x1fx7f-x9f]/g;
var _meta = {
'b': '\b',
't': '\t',
'n': '\n',
'f': '\f',
'r': '\r',
'"' : '\"',
'\': '\\'
};
})(jQuery);
js怎么把list转换json字符串?
1.List list = new Arr***List。
2.list.add(测试1)。
3.list.add(测试2)。
4.list.add(测试3)。
5.JSONArr*** jsonArr*** JSONArr***.fromObject(list)。
6.System.out.println(jsonArr***.toString。
7.这边要导包需要jsonlib.jarnet.sf.json.JSONArr***。
用java初始化一个list,向list中添加值,并输出list中的值:
1.import java.util.
2.public class .
3. public static void main(String arg[]) .
4. List lis = new Arr***list.
5. lis.add("wo").
6. lis.add("shi").
7. lis.add("wang").
8.for(int i=0;ilis.size(); i++).
9. System.out.print(lis.get(i)+"").
js怎么把数组转换成json字符串
1、javascript里面的数组都是关联数组吧?js对象的本质就是关联数组。索引数组也就是常说的数组,数组是对象,所以数组的本质也是关联数组,但通常没人这么说。2、唯一区别就是:索引数组的索引只能是0和正整数,但它是有序的。关联数组的索引很宽松,但关联数组是无序的,就这个。3、json对象是严格版本的关联数组。4、至少在chrome下,索引数组和关联数组的速度是一样的。5、索引数组的好处就是格式简单,关联数组的好处是键可以任意定义。现代浏览器中提供了JSON.stringify()方法将数组,对象转成json。JSON.stringify把一个对象转换成json字符串,JSON.parse把一个json字符串解析成对象。不支持的可以引入json2.js$.fn.stringifyArr***=function(arr***){returnJSON.stringify(arr***)}$.fn.parseArr***=function(arr***){returnJSON.parse(arr***)}然后调用:$("").stringifyArr***(arr***)
JS,实现一维数组JSON树结构的转换
1、方法思路使用js数组自带的filter()方法;数据格式要求,父子节点通过,Id,ParentId进行关联。默认父结节id为0。
样例数据:var jsonData = [{"id":"1","pid":"0","name":"家用电器"}, {"id":"4","pid":"1","name":"大家电"}, {"id":"5","pid":"1","name":"生活电器"}, {"id":"2","pid":"0","name":"服饰"}, {"id":"3","pid":"0","name":"化妆"}, {"id":"7","pid":"4","name":"空调"}, {"id":"8","pid":"4","name":"冰箱"}, {"id":"9","pid":"4","name":"洗衣机"}, {"id":"10","pid":"4","name":"热水器"}, {"id":"11","pid":"3","name":"面部护理"}, {"id":"12","pid":"3","name":"口腔护理"}, {"id":"13","pid":"2","name":"男装"}, {"id":"14","pid":"2","name":"女装"}, {"id":"15","pid":"7","name":"海尔空调"}, {"id":"16","pid":"7","name":"美的空调"}, {"id":"19","pid":"5","name":"加湿器"}, {"id":"20","pid":"5","name":"电熨斗"}];
2、实现方法:
function creatTreeData(list){
var clonelist = JSON.parse(JSON.stringify(list));
var result =clonelist.filter(function(father){
var childlist = clonelist.filter(function(child){
return father.id= child.pid;
});
if(childlist.length0){
father.children=childlist;
}
return father.pid ==0;
});
return result;
}
3、方法封装
function treeData(list,id,pid,children){
var clone = JSON.parse(JSON.stringify(list));
return clone.filter(function(father){
var childlist =clone.filter(function(child){
return father[id]==child[pid];
});
if(childlist.length0){
father[children]=childlist;
}
return father[pid]==0;
});
}
js转json的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于js转json字符、js转json的信息别忘了在本站进行查找喔。