Extjs 如何解码一个 json 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10912590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:22:14  来源:igfitidea点击:

Extjs how to decode a json string?

extjsdecodejson

提问by Pierluigi B Web Developer

I have to decode JSON with Extjs 4:

我必须使用以下方法解码 JSON 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:

我使用过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
    }]
}';

as you can see the field jsonfieldsis a JSON string. When I use

如您所见,该字段jsonfields是一个 JSON 字符串。当我使用

Ext.decode(string, true);

nothing happens neither error.

没有发生任何错误。

Any suggestions?

有什么建议?

回答by Vytautas

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 jsonfieldsthats because your string is in 'quotes and one \does the job for it but you want something different... so you have to double it.

这有点棘手。如果您删除安全参数,您将看到您的 json\在您的那个中丢失,jsonfields因为您的字符串在'引号中,并且有人\为它完成了这项工作,但您想要一些不同的东西......所以你必须将它加倍。

fiddle example

小提琴示例

回答by Djordje Arsenovic

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

它确实有效,例如我从服务器获取我的 Json,

websocket.onmessage = function(event)

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

从 websocket 实际上和稍后当我想解码我的 json 时,

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

and where I need my string for example

例如,我需要我的字符串的地方

json.map.MessageType

My json looks like this:

我的 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"}}

Hope this helps, cheers!

希望这有帮助,干杯!