admin管理员组

文章数量:1320656

I have a json string which is

I got this string by converting

var json = orgetd.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.

I have a json string which is

I got this string by converting

var json = org.etd.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.

Share Improve this question edited Feb 7, 2013 at 5:43 Uttara 2,5323 gold badges25 silver badges35 bronze badges asked Feb 7, 2013 at 5:19 Er KK ChopraEr KK Chopra 1,8508 gold badges33 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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);
json.replace('\\', '')

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

Use javascript replace function

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

本文标签: javascriptreplace string of json with another stringStack Overflow