Pierluigi B Web Developer Pierluigi B Web Developer - 3 months ago 22
JSON Question

Extjs how to decode a json string?

I have to decode json code with Extjs 4 :
I have used Ext.decode(string, true), but i doesn't work 'cause my string is a json with a json string (escaped) inside... like this:

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 field jsonfields is a json string. When I use

Ext.decode(string, true);

nothing happens neither error.

Any suggests?


You may try like this:

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);

that's a little bit tricky. If you remove safe parameter you will see that your json misses \ in your jsonfields thats because your string is in ' quotes and one \ does the job for it but you want something different... so you have to double it.

fiddle example