How to get/list all field names of a JSON data with ExtJS?

半城伤御伤魂 提交于 2019-12-24 11:51:00

问题


I have this json data:

[
{"ID":"1","name":"google","IP":"69.5.33.22","active":"true"},
{"ID":"2","name":"bing","IP":"70.5.232.33","active":"false"}
]

I want to get the all property names (?), like:

name,ID,IP,active

Note: I don't mind what is there at "name" and "IP" like google, 70.5.232.33, etc. Just, I want to get the fields itself.


回答1:


var jsonString = [
    {"ID":"1","name":"google","IP":"69.5.33.22","active":"true"},
    {"ID":"2","name":"bing","IP":"70.5.232.33","active":"false"}
];
var keyArray = Ext.Object.getKeys(Ext.JSON.decode(jsonString));
console.log(keyArray) // ["ID","name","IP","active"]



回答2:


Iterate over the object after you've parsed it.

js> for(i in {"ID":"1","name":"google","IP":"69.5.33.22","active":"true"})
{
  print(i);
}
ID
name
IP
active
js> 


来源:https://stackoverflow.com/questions/7370828/how-to-get-list-all-field-names-of-a-json-data-with-extjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!