[英]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
功能中讀取dis
和val
。 我alert(newValue)
看起來像[object Object],[object Object],[object Object]
編輯
alert(newValue[0]);
值是[
。 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.