簡體   English   中英

如何在Extjs 4中讀取對象

[英]How to read object in Extjs 4

我有一個formpanel文本字段的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}
           ]
   }
}

如何在change功能中讀取disval alert(newValue)看起來像[object Object],[object Object],[object Object]

編輯

  1. 但是我嘗試alert(newValue[0]); 值是[
  2. 我打印newValue類型,例如alert(typeof newValue); 結果是string ,我嘗試將其轉換為json

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

但是我得到了錯誤

SyntaxError: missing ] after element list
([object Object],[object Object],[object Object])

您可以使用以下方法將JSON字符串解析為JavaScript對象

var json = Ext.decode(jsonString);

然后,您可以像這樣訪問成員:

console.log(json.success);

我找到了解決方案。 我的json必須看起來像

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM