将javascript中的JSON输出为不带jquery的列表

Output JSON in javascript as a list without jquery

本文关键字:jquery 列表 javascript 中的 JSON 输出      更新时间:2023-09-26

我有一个JSON对象:

var txt = '{"employees":[' +
'{"firstName":"John","lastName":"Doe","time":"9:15am","email":"john_doe@gmail.com" },' +
'{"firstName":"Anna","lastName":"Smith","time":"9:15am","email":"anna@gmail.com" },' +
'{"firstName":"Peter","lastName":"Jones" ,"time":"9:15am","email":"peter@gmail.com"}]}';

我想打印为div语句之间的列表:

  • John Doe
  • 上午9:15
  • john_doe@gmail.com

  • Anna Smith

  • 上午9:15
  • anna@gmail.com

  • 彼得·琼斯

  • 上午9:15
  • peter@gmail.com

我试图填充的HTML如下所示:

    <div class="info">
        <ul>
            <li id="name"></li>
            <li id="time"></li>
            <li id="email"></li>
        </ul>
    </div>

我该如何做到这一点?

<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<div id="output">
</div> 
<script type="text/javascript">
var txt = '{"employees":[' +
'{"firstName":"John","lastName":"Doe","time":"9:15am","email":"john_doe@gmail.com" },' +
'{"firstName":"Anna","lastName":"Smith","time":"9:15am","email":"anna@gmail.com" },' +
'{"firstName":"Peter","lastName":"Jones" ,"time":"9:15am","email":"peter@gmail.com"}]}';
var employees=JSON.parse(txt).employees;
var container=document.getElementById("output");
for (i=0;i<employees.length;i++) { //Loops for the length of the list
    var info=document.createElement('div');
    info.className='info'; //Creates a new <div> element and adds the class info to it
    var ul=document.createElement('div'); //Creates <ul> element
    info.appendChild(ul); //Adds the <ul> to the newly created <div>
    var name=document.createElement('li');
    name.className='name'; //Should use class, not id, as ID must be unique
    name.innerHTML=employees[i].firstName+' '+employees[i].lastName; //Adds name
    ul.appendChild(name);
    var time=document.createElement('li');
    time.className='time';
    time.innerHTML=employees[i].time;
    ul.appendChild(time);
    var email=document.createElement('li');
    email.className='email';
    email.innerHTML=employees[i].email;
    ul.appendChild(email);
    container.appendChild(info); //Adds the final generated HTML to the page
} //Will repeat for each item in list.
</script>
</body>
</html>

首先,将字符串转换为对象:

var employees=JSON.parse(txt).employees;

然后,设置一个循环来构建HTML

var container=//Link to the containing element using getElementById or similar
for (i=0;i<employees.length;i++) { //Loops for the length of the list
    var info=document.createElement('div');
    info.className='info'; //Creates a new <div> element and adds the class info to it
    var ul=document.createElement('div'); //Creates <ul> element
    info.appendChild(ul); //Adds the <ul> to the newly created <div>
    var name1=document.createElement('li'); //Chrome seems not to like the variable "name" in this instance
    name1.className='name'; //Should use class, not id, as ID must be unique
    name1.innerHTML=employees[i].firstName+' '+employees[i].lastName; //Adds name
    ul.appendChild(name1);
    var time=document.createElement('li');
    time.className='time';
    time.innerHTML=employees[i].time;
    ul.appendChild(time);
    var email=document.createElement('li');
    email.className='email';
    email.innerHTML=employees[i].email;
    ul.appendChild(email);
    container.appendChild(info); //Displays the elements on the page
} //Will repeat for each item in list.

这也可以使用container.innerHTML来实现,但构造这样的元素可能会混淆DOM,因此通常只建议用于文本节点,尽管理想情况下应该使用document.createTextNode()

首先通过解析JSON字符串中的数组:

var employees = JSON.parse(txt).employees

然后创建像这样的HTML元素

var element = document.createElement('div'); // creates an empty div

然后你把HTML树和放在一起

someElement.append(otherElement); // makes otherElement a child of someElement