您好我想将json对象转换为特殊字符转义的字符串.
以下是示例
{
"xtype": "window",
"height": 250,
"width": 400,
"bodyPadding": "20 0 0 20",
"title": "Configuration",
"modal": true,
"items": [
{
"xtype": "textfield",
"fieldLabel": "Deploy Path"
},
{
"xtype": "textfield",
"fieldLabel": "Save Path"
},
{
"xtype": "button",
"margin": "20 0 0 100",
"text": "Save"
}
]
}
Run Code Online (Sandbox Code Playgroud)
以上对象
{\n \"xtype\": \"window\",\n \"height\": 250,\n \"width\": 400,\n \"bodyPadding\": \"20 0 0 20\",\n \"title\": \"Configuration\",\n \"modal\": true,\n \"items\": [\n {\n \"xtype\": \"textfield\",\n \"fieldLabel\": \"Deploy Path\"\n },\n {\n \"xtype\": \"textfield\",\n \"fieldLabel\": \"Save Path\"\n },\n {\n \"xtype\": \"button\",\n \"margin\": \"20 0 0 100\",\n \"text\": \"Save\"\n }\n ]\n}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
提前致谢.
嗨,
我的JSON包含一些添加的插件,因为stringify函数不起作用.例如
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
ptype: 'cellediting'
})
]
Run Code Online (Sandbox Code Playgroud)
这是事情不适合我的事情,我希望有人可以帮助我.
提前致谢.
我不确定你为什么会这么想.目标是构建一个可以在程序中编写的字符串文字吗?
但是,无论如何,这似乎是这样做的:
var str = JSON.stringify(obj, null, '\n')
.replace(/"/g, '\\"')
.replace(/\n/g, ' ')
.replace(/(?:[ ]{4}((?:[ ]{4})*))/g, '\\n$1');
Run Code Online (Sandbox Code Playgroud)
请注意,您必须开始的不是"JSON对象"(这意味着JSON不是数据交换格式),而是一个简单的javascript对象.