Er KK Chopra
Er KK Chopra

Reputation: 1850

replace string of json with another string

I have a json string which is

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 ]

Help me if posible.

Upvotes: 4

Views: 12380

Answers (3)

asifsid88
asifsid88

Reputation: 4711

Use javascript replace function

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

Upvotes: 0

Pratik Bhatt
Pratik Bhatt

Reputation: 911

You can simple use 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);

Upvotes: 3

Explosion Pills
Explosion Pills

Reputation: 191809

json.replace('\\', '')

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

Upvotes: 0

Related Questions