如何读取Extjs 4中的对象

时间:2013-10-13 05:43:37

标签: extjs extjs4.1

我有formpanel有文本字段

         {
            xtype: 'textfield',
            name: name,
            listeners: {
                change: function( field, newValue, oldValue, eOpts ){
                    alert(newValue) // [object Object],[object Object],[object Object]
                }
            }
         }

我使用form.load({...});将值加载到textfield

这是我的json

{
 "success":true,
  "data":{
          "name":[
                 {"dis":3,"val":0},
                 {"dis":2,"val":1},
                 {"dis":1,"val":2}
           ]
   }
}

如何阅读dis函数中的valchange。我alert(newValue)看起来像 [object Object],[object Object],[object Object]

修改

  1. 但我尝试alert(newValue[0]);值为[
  2. 我打印类型为newValue的{​​{1}}结果为alert(typeof newValue);,我尝试将其转换为json,如
  3. string

    但我收到错误

    var json = eval("(" + newValue + ")");

2 个答案:

答案 0 :(得分:0)

您可以使用以下

将JSON字符串解析为JavaScript对象

var json = Ext.decode(jsonString);

然后您可以访问这些成员:

console.log(json.success);

答案 1 :(得分:0)

我找到了解决方案。我的json必须看起来像

{
 "success":true,
  "data":{
          "name":[
                 {\"dis\":3,\"val\":0},
                 {\"dis\":2,\"val\":1},
                 {\"dis\":1,\"val\":2}
           ]
   }
}

之后使用Ext.decode(newValue);:)