我从服务器字符串中获取如下内容:
{"Point Ref":[15629989,564646414,65494949],
"Effective Date":["2008-03-03","2010-12-14","2004-10-01"],
"Identifier":["EM","EM","SC"],"Status":["FI","SI","XC"]}"
我将其转换为JSON
:
var jsonResponse = jQuery.parseJSON(xmlHttp.responseText.trim());
直到这里一切都很好:我可以循环jsonResponse
并完成我的工作。但是,我找不到一种简单快捷的方法,可以立即将所有密钥放在一个array
中。
基本上,还有除此之外的其他内容:
var keys = new Array();
var n = 0
for (var i in jsonResponse){
keys[n] = i
n ++
}
感谢。
答案 0 :(得分:6)
您可以使用Object.keys:
var keys = Object.keys(jsonResponse);
答案 1 :(得分:0)
//minified polyfill for old browsers:
if (!Array.prototype.map) { Array.prototype.map = function (e, t) { var n, r, i; if (this == null) { throw new TypeError(" this is null or not defined") } var s = Object(this); var o = s.length >>> 0; if (typeof e !== "function") { throw new TypeError(e + " is not a function") } if (t) { n = t } r = new Array(o); i = 0; while (i < o) { var u, a; if (i in s) { u = s[i]; a = e.call(n, u, i, s); r[i] = a } i++ } return r } }
var keys = jsonResponse.map(function(v, k) { return k; });