javascript 用另一个字符串替换 json 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14744101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:28:21  来源:igfitidea点击:

replace string of json with another string

javascriptjson

提问by Er KK Chopra

I have a json string which is

我有一个 json 字符串,它是

I got this string by converting

我通过转换得到了这个字符串

var json = org.cometd.JSON.toJSON(envelope.messages);

"[{\"version\": \"1.0\", \"minimumVersion\": \"0.9\", \"channel\": \"/meta/handshake\", \"supportedConnectionTypes\": [\"long-polling\", \"callback-polling\"], \"advice\": {\"timeout\": 60000, \"interval\": 0}, \"id\": \"1\"}]"

and I need to replace some symbols, I need output like that

我需要替换一些符号,我需要这样的输出

[{"version":"1.0","minimumVersion":"0.9","channel":"/meta/handshake","supportedConnectionTypes":["long-polling","callback-polling"],"advice":{"timeout":60000,"interval":0},"id":"1"}]

means symbols to be replaced are \\with ""and "[with [and ]"with ]

表示要替换的符号是 \\with"""[with[]"with]

Help me if posible.

如果可能,请帮助我。

回答by Pratik Bhatt

You can simple use Json.Parse()

你可以简单的使用 Json.Parse()

var json = "[{\"version\": \"1.0\", \"minimumVersion\": \"0.9\", \"channel\": \"/meta/handshake\", \"supportedConnectionTypes\": [\"long-polling\", \"callback-polling\"], \"advice\": {\"timeout\": 60000, \"interval\": 0}, \"id\": \"1\"}]"

JSON.Parse(json);

回答by asifsid88

Use javascript replace function

使用 JavaScript replace function

mystring.replace(/\/g,'').replace(/" "[ "/g,'"["')

回答by Explosion Pills

json.replace('\', '')

There are no "[in the string itself, there's just "that define the string.

"[字符串本身没有,只有"定义字符串。