Roysh Roysh - 5 months ago 42
jQuery Question

Convert Json to ul and li

I Have an object that looks like this:

{
"qA": [
{
"question": "How deep is the ocean",
"answer": [
"quite deep",
"very deep",
"no deep at all"
]
},
{
"question": "How high is the sky",
"answer": [
"real high",
"high enough",
"not that high"
]
}
]
}


I want to load it into a
ul
where the question is the title of the
ul
and the answers are its
li
. How do I do that?

Answer

you can do with jquery like this:

$.each(data.qA, function (index, item) {
    //console.log(item);
    html += "<ul>" + item.question;

    $.each(item.answer, function (index1, item1) {
        html += "<li>" + item1 + "</li>";

    });
    html+="</ul>";
});
$("#container").append(html);

FIDDLE DEMO