我必须使用Extjs 4
解码JSON:
我使用了Ext.decode(string, true)
,但它不起作用'因为我的字符串是一个带有JSON字符串(转义)的JSON ......就像这样:
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
}]
}';
正如您所见,字段jsonfields
是一个JSON字符串。当我使用
Ext.decode(string, true);
没有任何错误发生。
有什么建议吗?
答案 0 :(得分:10)
您可以尝试这样:
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);
这有点棘手。如果你删除安全参数,你会发现你的\
中你的json错过了jsonfields
,因为你的字符串在'
引号中而且\
为你完成了工作但是你想要不同的东西...所以你必须加倍。
答案 1 :(得分:1)
它确实有效,例如我从服务器获取Json,
websocket.onmessage = function(event)
实际上来自websocket的以及稍后当我想解码我的json时,
var json = Ext.decode(event.data);
以及我需要我的字符串的地方
json.map.MessageType
我的json看起来像这样:
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"}}
希望这有帮助,欢呼!