0

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
    });
}
1
  • 2
    Try refTopic['refTopic'][0]['REFTOPICID'] Commented Jul 9, 2015 at 11:14

1 Answer 1

2

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);   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.