问题
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