How to reference a particular JSON in a function call.

Posted by Jane Wilkie on Stack Overflow See other posts from Stack Overflow or by Jane Wilkie
Published on 2011-01-01T00:29:02Z Indexed on 2011/01/01 0:54 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

Hi guys! I have two javascript routines.. the first one declares some JSON and it contains a function that takes two arguments, the first argument being the json object that needs traversing and the second argument is the tab that the rendering is done in. The second routine merely passes the name of the JSON that needs traversing and tab to render in. The code is below....

<script language="JavaScript1.2" type="text/javascript">

var arr = [ {"id":"10", "class":"child-of-9", "useless":"donotneed"}, {"id":"11",    "class":"child-of-10", "useless":"donotneed"}];
var arrtwo = [ {"id":"12", "class":"child-of-12", "useless":"donotneed"}, {"id":"13", "class":"child-of-13", "useless":"donotneed"}];

function render_help(json,tab){
var html='';
    for(var i=0;i<json.length;i++){
        var obj = json[i];
        for(var key in obj){
            var attrName = key;
            var attrValue = obj[key];
            if (attrName == "id"){
                html = html +'<B>'+attrValue+'</B>'+'<BR><BR>';
            }else if (attrName == "class"){
                html = html + attrValue + '<BR><BR>';
            }
        }
    }
document.getElementById(tab).innerHTML=(html);
}
</script>

<script language="JavaScript1.2" type="text/javascript">
render_help(arr,"helptab");
</script>

Various testing and strategically placed alert boxes indicate that the tab parameter is being passed and interpreted correctly. I know this because when I change ....

document.getElementById(tab).innerHTML=(html);

to

document.getElementById(tab).innerHTML=("Howdy");

and it renders "Howdy" just fine. Putting an alert box (alert(json)) in to check the value of json yields....

[object.Object],[object.Object] 

The JSON object remains elusive. For the purposes of this scripting I need the JSON "arr" to be iterated over. I feel like the answer is fairly obvious so far no luck.

Admittedly I am new with Javascript and I am apparently missing something. Does anyone have a clue as to what I'm overlooking here?

Happy New Year to you all! Janie

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON