javascript - JSON 中的 JSON 字符串

标签 javascript json escaping stringify

我想在 JSON 请求中创建一个 JSON 字符串。这是我的代码,

Fiddle

JS

var x = {
    a: 1,
    b: 'a sample text',
};

var request = {
    t: JSON.stringify(x),
    c: 2,
    r: 'some text'
};

console.log(request);

谁能帮我转义双引号?

控制台

Object {
  t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes.
  c: 2, 
  r: "some text"
}

提前致谢。

最佳答案

没有问题。这只是您的 console.log,它通过简单地用 " 分隔来显示所有字符串。

正如您所说,此 request 对象在 JSON 请求中使用,它将在另一次 JSON.stringifyed 中得到有效结果

{"t":"{\"a\":1,\"b\":\"a sample text\"}","c":2,"r":"some text"}

关于javascript - JSON 中的 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26238796/

相关文章:

javascript - Angular-UI( Bootstrap )弹出窗口不起作用?

json - 在复制数据中从 Source 到 Sink 丢失数据

php - 如何反转 escapeshellarg?

Javascript 或 Flash 导出到 CSV/Excel

javascript - getJSON 和 mod 重写

xpath - 如何在 WSO2 属性 XPath 表达式中转义撇号

java - 如何在 spring 的 bean 属性值中包含 < 或 > 字符?

javascript - 匿名闭包函数内的“this”

javascript - 在 VueJs + SlimPHP 中输​​入图像文件

javascript - fullpage.js - 在移动设备上设置 responsiveWidth 和 scrollOverflow 中断页面