4

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.

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

4 Comments

not working :( var json = org.cometd.JSON.toJSON(envelope.messages); JSON.Parse(json); alert(json);
if you alert its result you will get [object obejct' check by logging it using console.log
why you are alerting your json? and if possible create your fiddle so that I can update that ....!!
var json = org.cometd.JSON.toJSON(envelope.messages); JSON.Parse(json); _webSocket.send(json); this._debug('Transport', this.getType(), 'sent', envelope, 'metaConnect =', metaConnect); // Manage the timeout waiting for the response var maxDelay = this.getConfiguration().maxNetworkDelay; var delay = maxDelay; if (metaConnect) { delay += this.getAdvice().timeout; _connected = true; }
0
json.replace('\\', '')

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

3 Comments

and also in the starting and ending i wants to remove " this symbol
@krishan what is your goal exactly?
replaced are \\ with "" and "[ with [ and ]" with ]
0

Use javascript replace function

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.