2

I have this JSON string that I'm getting through with $.getJSON:

{
    "Menu1":"Item1",
    "Menu2": {
        "SubMenu1":"SubItem1",
        "SubMenu2":"SubItem2"
    },
    "Menu3":"Item3",
    "Menu4": {
        "SubMenu2": {
            "SubSubMenu1":"SubSubItem1"
        }
    }
}

How can I loop through to represent a menu such as:

<ul>
    <li>Item1</li>
    <li>Menu2
        <ul>
            <li>SubItem1</li>
            <li>SubItem2</li>
        </ul>
    </li>
    <li>Item3</li>
    <li>Menu4
        <ul>
            <li>SubMenu2
                <ul>
                    <li>SubSubItem1</li>
                </ul>
            </li>
        </ul>
     </li>
</ul>
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
Or Weinberger
  • 7,332
  • 23
  • 71
  • 116
  • 1
    What's the trouble with your accepted answer to your other question: [jQuery JSON looping through nested objects](http://stackoverflow.com/questions/8553539/jquery-json-looping-through-nested-objects)? – RightSaidFred Dec 18 '11 at 20:03
  • @RightSaidFred The other answer solves only a part of my question.. I'm trying to get the Key in the
  • in case it's a nested object
  • – Or Weinberger Dec 18 '11 at 20:05
  • Did you try on your own? All you need is to change this `$("
  • ").appendTo(ul)` to this `$("
  • ").append(key).appendTo(ul)`. http://jsfiddle.net/uXww2/1/
  • – RightSaidFred Dec 18 '11 at 20:09
  • You're welcome. [Here's an example](http://jsfiddle.net/uXww2/2/) using your new data. – RightSaidFred Dec 18 '11 at 20:14