7

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?

4

2 回答 2

10

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

于 2012-06-06T15:11:09.407 回答
1

It does work, for example I am getting my Json from the server,

websocket.onmessage = function(event)

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

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

and where 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!

于 2016-10-04T15:52:09.160 回答