سؤال

Iam having a Json object in the below format

var obj={"results": [{"division": "xxx", "xxx": "Administration", "id": "0001", "designation": "Chief Managing Director", "name": "Dr. xxx xx xx"}]};

when am looping over this json by using

for (var i in obj.results){
}

when am debugging it is giving me obj.results is undefined.

Please help

هل كانت مفيدة؟

المحلول

 var obj = { "results": [{ "division": "xxx", "xxx": "Administration", "id": "0001", "designation": "Chief Managing Director", "name": "Dr. xxx xx xx"}] };

        for (var i in obj.results) {
        }

Its working

نصائح أخرى

Here your json is not in correct format. you are missing closing braces. It should be like this :

var obj={"results": [{"division": "xxx", "xxx": "Administration", "id": "0001", "designation": "Chief Managing Director", "name": "Dr. xxx xx xx"}]}

Then your for loop will work properly.

for (var i in obj.results){
}

Note that, obj.results isn't an array.

I think you are needing this:

for(var i in Object.getOwnPropertyNames(obj.results)){

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top