Reputation: 37
With "data" being JSON, why does this script not work? I am using "$.parseJSON(data);" to convert the JSON to an array, and the last line of code is how I would typically access the resulting array.
{
"refTopic": [
{
"REFTOPICABV": "Purpose",
"REFTOPICVALUE": "Purpose and Need",
"REFTOPICID": 65
},
{
"REFTOPICABV": "Description",
"REFTOPICVALUE": "Project Description",
"REFTOPICID": 66
}
]
}
if (refTopic == undefined) {
getTopicsSelectBox(function(data) {
console.log(typeof data); //string
var refTopic = $.parseJSON(data);
console.log(typeof refTopic); //object
console.log(refTopic instanceof Array); //false
console.log(refTopic[i].REFTOPICID); //undefined
});
}
Upvotes: 0
Views: 50
Reputation: 17064
You are missing one level of referencing, it should be:
refTopic.refTopic
This is because you wrote:
var refTopic = $.parseJSON(data);
, so the variable is the entire object, not specifically refTopic
inside it.
I'd just write like this to be more clear:
var refTopicObj = $.parseJSON(data);
console.log(typeof refTopicObj);
console.log(refTopicObj.refTopic instanceof Array);
console.log(refTopicObj.refTopic[i].REFTOPICID);
Upvotes: 2