我有JSON
data = {
"408": "NameSurname1",
"415": "NameSurname2"
}
我如何用jQuery解析它
<ol>
<li>
<p>408</p> , <p>NameSurname1</p>
</li>
<li>
<p>415</p> , <p>NameSurname2</p>
</li>
</ol>
答案 0 :(得分:1)
我认为最简单的方法是:
var stringBuilder = "";
for (var key in data) {
stringBuilder += "<li><p>" + key + "</p>, <p>" + data[key] + "</p></li>";
}
$('yourOL').html(stringBuilder);
答案 1 :(得分:0)
只需使用for
循环访问对象并附加li
元素
data = {
"408": "NameSurname1",
"415": "NameSurname2"
}
for (i in data) {
$('ol').append('<li><p>' + i + '</p><p>' + data[i] + '</p></li>');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ol></ol>
&#13;