JSON中的JSON字符串 [英] JSON String inside a JSON

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

问题描述

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

I want to create a JSON string inside a JSON request. Here is my code,

小提琴

JS

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

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

console.log(request);

有人可以帮助我如何逃避双引号?

Can someone help me how to escape the double quotes?

控制台

Console

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

感谢提前。

推荐答案

这就是浏览器控制台显示一个字符串的值,通过用双引号括起来输出。这是完全正常的,没有任何破坏。

That's just the way the browser console shows you the value of a string, by wrapping in double quotes for the output. This is perfectly normal and nothing is broken.

您可以通过将JSON字符串转换回对象并使用属性来测试。

You can test it by transforming your JSON string back to an object and using a property.

console.log( JSON.parse(request.t).b ); // a sample text

这篇关于JSON中的JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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