Extjs how to decode json string?

I need to decode JSON using Extjs 4 :

I used Ext.decode(string, true) , but this does not work because my string is JSON with a JSON string (escaped) inside ... for example:

 var string = '{ success: true, rows: [{ "id": 33, "defaultset": 1, "name": "Generico", "jsonfields": "[{\"name\":\"cm:addressees\",\"title\":\"Destinatari\",\"description\":\"Destinatari\",\"dataType\":\"d:text\",\"url\":\"\/api\/property\/cm_addressees\"}]", "eliminato": 0 }] }'; 

as you can see the jsonfields is a JSON string. When i use

 Ext.decode(string, true); 

nothing happens with an error.

Any suggestions?

+7
source share
2 answers

You can try the following:

 var string = '{success:true, rows:[{"id":33,"defaultset":1,"name":"Generico","jsonfields":"[{\\"name\\":\\"cm:addressees\\",\\"title\\":\\"Destinatari\\",\\"description\\":\\"Destinatari\\",\\"dataType\\":\\"d:text\\",\\"url\\":\\"/api/property/cm_addressees\\"}]","eliminato":0}]}'; var decodedString = Ext.decode(string); console.log(decodedString); 

which is a bit complicated. If you remove the safe option, you will see that your json skips \ in your jsonfields because your string is in quotation marks ' and one \ is doing the job, but you want something else ... so you have to double it.

script example

+10
source

This works, for example, I get my Json from the server,

 websocket.onmessage = function(event) 

from websocket actually and later, when I want to decode my json,

 var json = Ext.decode(event.data); 

and where do I need my string for example

 json.map.MessageType 

My json looks like this:

 mpty":false,"map":{"MessageText":"Ciao, how are you?","MessageType":"IN"},"hashtable":{"MessageText":"Ciao, how are you?","MessageType":"IN"},"persistData":{"MessageText":"Ciao, how are you?","MessageType":"IN"}} 

Hope this helps, cheers!

+1
source

All Articles