3

Hi i want to convert the json object to a string with special characters escaped.

following is the example

{
"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"
    }
]
}

above object to

{\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}

can anybody please help me here ?

Thanks in advance.

Hi,

My JSON contains some added plugins because of which the stringify function is not working. for example

plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
ptype: 'cellediting'
})
]

This is something where the things are not working for me , and i hope somebody can help me out here.

Thanks in advance.

6
  • What have you tried? -> JSON.parse ??? -> developer.mozilla.org/en-US/docs/JavaScript/Reference/… Commented Mar 6, 2013 at 14:04
  • 4
    This is not the same question. OP asks for escaping certain characters. Stop voting to close this question. Commented Mar 6, 2013 at 14:06
  • 1
    Also JSON.parse is completely wrong, stringify would be correct. Commented Mar 6, 2013 at 14:12
  • Are you trying to build a string literal to put in another program ? Commented Mar 6, 2013 at 14:15
  • 2
    Once again, this is not a duplicate. Learn to read people! Commented Mar 6, 2013 at 14:24

2 Answers 2

8

I'm not sure of why you might want this. Is the goal to build a string literal that you could write in a program ?

But, anyway, this seems to do it :

var str = JSON.stringify(obj, null, '\n')
          .replace(/"/g, '\\"')
          .replace(/\n/g, '    ')
          .replace(/(?:[ ]{4}((?:[ ]{4})*))/g, '\\n$1');

Note that what you have to start with isn't a "JSON object" (which means nothing as JSON is a data-exchange format) but a plain javascript object.

jsbin example

Sign up to request clarification or add additional context in comments.

3 Comments

I've altered the solution to what, I believe, is more closely to what OP is after. Hope you don't mind.
@fireeyedboy No, I really don't mind. You're probably right (hard to be sure with OP not responding).
@destroy : yes , the goal here is to use the string in another program. i have already converted the JSON object to a string using JSON.stringify and then passing the converted string to a replace function. but i was not sure of a regex to use. will use this and reply. Thanks
0

Using org.json library:

here is one sample code.

JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");

or

var j={"name":"phonetype"};
JSON.stringify(j); // '{"name":"phonetype"}'

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.