ben*_*nVG 4 javascript jquery json properties object
序言:我是意大利人,抱歉我的英语不好.
我需要使用javascript/jquery从json对象中检索属性的名称.
例如,从这个对象开始:
{
"Table": {
"Name": "Chris",
"Surname": "McDonald"
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获得字符串"姓名"和"姓氏"?
就像是:
//not working code, just for example
var jsonobj = eval('(' + previouscode + ')');
var prop = jsonobj.Table[0].getPropertyName();
var prop2 = jsonobj.Table[1].getPropertyName();
return prop + '-' + prop2; // this will return 'Name-Surname'
Run Code Online (Sandbox Code Playgroud)
elc*_*nrs 10
var names = [];
for ( var o in jsonobj.Table ) {
names.push( o ); // the property name
}
Run Code Online (Sandbox Code Playgroud)
在现代浏览器中:
var names = Object.keys( jsonobj.Table );
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24626 次 |
最近记录: |