在javascript中遍历json响应

时间:2013-09-30 09:16:47

标签: javascript

我有这个json响应,其中obj.attributes列出以下内容。我想通过每个对象只有inturn有id ..我如何遍历对象和print id属性

Object {0: Object, 1: Object, 2: Object, 3: Object, subject_Keys: "", schoolapplication: Object}

我试过

for(var item in obj.attributes)
{
     var intRegex = /^\d+$/;
 if(intRegex.test(item)) {
       console.log(obj.attributes[item]["id"]) //This prints undefined
     }
}

EDIT1:console.log(obj)

child {cid: "c33", changed: Object, attributes: Object, _changes: Array[0], _hasComputed: true…}
_changes: Array[0]
_changing: false
_currentAttributes: Object
_hasComputed: true
_pending: false
_previousAttributes: Object
app_id: 8
attributes: Object
changed: Object
cid: "c33"
__proto__: Surrogate

的console.log(obj.attributes)

 Object {0: Object, 1: Object, 2: Object, 3: Object, subject_Keys: "", schoolapplication: Object}  
 0: Object
   name: "key1"
   schoolapplication: Object
   id: 3
   __proto__: Object
1: Object
2: Object
3: Object
__proto__: Object

1 个答案:

答案 0 :(得分:0)

如果你给整个对象提供数据,那么它会有所帮助。但我明白我得到了这个解决方案

for(var item in obj)
{
    if(obj[item].hasOwnProperty('id')) {
        console.log(obj[item].id);
    }
}