JSON.stringify无需转义 [英] JSON.stringify escaping without need

查看:138
本文介绍了JSON.stringify无需转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON.stringify将我的json对象转换为以下字符串

JSON.stringify is converting my json object to the following string


{\2003 \:{\ 1 \:{\2 \:[\test \],\3 \:[\test2 \]}}}

{\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}}

什么时候不应该转义。结果应该是下面引用的字符串

When it should not be escaped. The result should be as the string quoted below


{2003:{1:{2:[test ],3:[test2]}}}

{"2003":{"1":{"2":["test"],"3":["test2"]}}}

而不是使用所有转义引号的一般替换并删除可能在输入中的那些。如何将JSON.stringify设置为不对变量进行双重转义?

Rather than use a general replace of all the escaped quotes and remove ones that could be in the input. How can I set JSON.stringify to not double escape the variables?

推荐答案

您正在字符串化字符串,而不是对象:

You are stringifying a string, not an object:

var str = '{"2003":{"1":{"2":["test"],"3":["test2"]}}}';
var obj = {"2003":{"1":{"2":["test"],"3":["test2"]}}};

console.log( JSON.stringify(str) );  // {\"2003\":{\"1\":{\"2\":[\"test\"],\"3\":[\"test2\"]}}} 
console.log( JSON.stringify(obj) );  // {"2003":{"1":{"2":["test"],"3":["test2"]}}} 

这篇关于JSON.stringify无需转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆