我正在使用Tit闭锁调用一个api,该api使用如下格式的JSON进行应答:
{
"1":{"id":"4","field1":"Name 1","ordering":"1"},
"2":{"id":"6","field1":"Name 2","ordering":"2"},
"3":{"id":"7","field1":"Name 3","ordering":"3"},
"4":{"id":"5","field1":"Name 4","ordering":"4"}
}我想用这样的代码来迭代这个答案:
json = JSON.parse(this.responseText);
for (var i=0; i<json.length; i++) {
//Something here with json[i];
}此代码不执行,因为json.length不是有效值。
在不改变服务器端JSON的情况下,我如何解决呢?
发布于 2014-06-01 17:50:41
使用for in循环循环对象中的每个属性:
for (var i in json)https://stackoverflow.com/questions/23982688
复制相似问题