0

i need display the child value, but it display [object, object] like that so

Here my code:

var jsonObj = {"department":{
              "Title1":[
                           {"child1":"Green",
                            "child2":"Yellow"
                           },

                           {"child3":"Black",
                            "child4":"White"
                           }
                           ],
              "Title2":[
                           {"child5":"Violet",
                            "child6":"purple"
                           },
                          {"child7":"Pink",
                            "child8":"Orange"
                           }
                           ]
  }
}
$(document).ready(function() {

var  treeList = "";
treeList = "<ul id=\"createTree\">";     
for(var key in jsonObj){
        for(var subKey in jsonObj[key]){
        alert(subKey);
        //for(i=0; i<jsonObj[key].length;i++ ) {
           treeList +=  ("<li>" + subKey + "<ul><li>"+jsonObj[key][subKey]+"</li></ul></li>");
           //var b = $(c).text();
           alert(treeList);
       }
    }
treeList += "</ul>";
$('#tree').append(treeList);
});
2
  • 1
    Consider writing a slightly more descriptive title to your question, short and concise, without necessarily including your entire question in said title... Commented Jul 22, 2011 at 10:22
  • 1
    There is no JSON in your code. I'd say you have a problem with JavaScript objects. Commented Jul 22, 2011 at 10:25

2 Answers 2

1

As Quentin said you need to keep drilling down, first into the array then into the objects contained within the array. The code below should access all the variables held in the JSON, you might have to restructure the HTML it outputs to get it looking as you want -

$(document).ready(function() {

var  treeList = "";
treeList = "<ul id=\"createTree\">";     
for(var key in jsonObj){
        for(var subKey in jsonObj[key]){
        alert(subKey);
        for(i=0; i<jsonObj[key][subKey].length;i++ ) {
           for(var arrayKey in jsonObj[key][subKey][i]){ 
              treeList +=  ("<li>" + subKey + " - " + arrayKey  + " - "+jsonObj[key][subKey][i][arrayKey]+"</li></ul></li>");
           }    
           //var b = $(c).text();
           alert(treeList);
        }   
       }
    }
    treeList += "</ul>";
    $('#tree').append(treeList);
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

The first jsonObj[key][subKey] will be jsonObj.department.Title1. This is an array.

When you stringify an array, it will, by default, produce the generic "This is an object" text.

If you want to display the data in it, you will have to keep going down to get at the strings.

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.