Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extjs how to decode a json string?

Tags:

json

decode

extjs

I have to decode JSON with Extjs 4:

I have used Ext.decode(string, true), but it 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 suggestions?

like image 297
Pierluigi B Web Developer Avatar asked Jun 06 '12 10:06

Pierluigi B Web Developer


People also ask

What is Ext encode?

try using Ext.encode(Object) it Encodes an Object, Array or other value & returns The JSON string.

What is Ext JSON?

GitHub - discoveryjs/json-ext: A set of performant and memory efficient utilities that extend the use of JSON.

Is JSON in node JS?

Introduction to JSON Despite its name, the use of the JSON data format is not limited to JavaScript. Most programming languages implement data structures that you can easily convert to JSON and vice versa. JavaScript, and therefore the Node. js runtime environment, is no exception.


1 Answers

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);
console.log(decodedString);

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

like image 161
Vytautas Avatar answered Sep 22 '22 18:09

Vytautas