简体   繁体   中英

How to read object in Extjs 4

I have a formpanel that has a textfield

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

I using form.load({...}); to load value to textfield

Here is my json

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

How can I read dis and val in change function. I alert(newValue) look like [object Object],[object Object],[object Object]

Edit

  1. But I try alert(newValue[0]); value is [ .
  2. I print type of newValue like alert(typeof newValue); result is string and I try to convert it to json like

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

But I get error

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

You can parse a JSON string into a JavaScript object using the following

var json = Ext.decode(jsonString);

Then you can access the members as such:

console.log(json.success);

I found solution. My json must be look like

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

And after that using Ext.decode(newValue); :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM