简体   繁体   English

如何将 JSON 数据转换为 HTML

[英]How to Convert JSON Data into HTML

I came across an exercise in freeCodeCamp to convert json data to html.我在freeCodeCamp遇到了一个将 json 数据转换为 html 的练习。 Here, I was asked to copy paste a jquery which I didn't understand.在这里,我被要求复制粘贴一个我不明白的 jquery。

 json.forEach(function(val) { var keys = Object.keys(val); html += "<div class = 'cat'>"; keys.forEach(function(key) { html += "<strong>" + key + "</strong>: " + val[key] + "<br>"; }); html += "</div><br>"; });

This is my json这是我的json

 [ { "id":0, "imageLink":"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg", "altText":"A white cat wearing a green helmet shaped melon on it's head. ", "codeNames":[ "Juggernaut", "Mrs. Wallace", "Buttercup" ] }, { "id":1, "imageLink":"https://s3.amazonaws.com/freecodecamp/grumpy-cat.jpg", "altText":"A white cat with blue eys, looking very grumpy. ", "codeNames":[ "Oscar", "Scrooge", "Tyrion" ] }, { "id":2, "imageLink":"https://s3.amazonaws.com/freecodecamp/mischievous-cat.jpg", "altText":"A ginger cat with one eye closed and mouth in a grin-like expression. Looking very mischievous. ", "codeNames":[ "The Doctor", "Loki", "Joker" ] } ]

Can anyone help me to break down this code and tell what each line in the code does?谁能帮我分解这段代码并告诉我代码中的每一行是做什么的? For example I don't know what Object.keys does.例如,我不知道 Object.keys 是做什么的。 Is Object an inbuilt instance? Object 是内置实例吗?

The Object.keys() method returns an array of a given object's own enumerable properties. Object.keys() 方法返回给定对象自己的可枚举属性的数组。

var keys = Object.keys(val);

Here 'keys' is the array form of your json.这里'keys'是你的json的数组形式。 According to the JSON you provided the array has 3 objects.根据您提供的 JSON,该数组有 3 个对象。

You can also write你也可以写

Object.keys(val).forEach(function(key){
  //something
});

instead of而不是

var keys = Object.keys(val);

keys.forEach(function(key) {
     //something
  });

Inside the loop the key returns the the key of your object ie id , imageLink etc and val[key] return corresponding values eg 0 , "https://s3.amazonaws.com/freecodecamp/funny-cat.jpg" to be more specific.在循环内, key返回对象的键,即idimageLink等,而val[key]返回相应的值,例如0"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg"更多具体的。

From MDN来自MDN

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. Object.keys() 返回一个数组,其元素是与直接在对象上找到的可枚举属性相对应的字符串。 The ordering of the properties is the same as that given by looping over the properties of the object manually.属性的顺序与通过手动循环对象的属性给出的顺序相同。

The purpose of the code is to generate html by using key and corresponding value .代码的目的是通过使用key和对应的value生成 html 。

 var json = [ { "id":0, "imageLink":"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg", "altText":"A white cat wearing a green helmet shaped melon on it's head. ", "codeNames":[ "Juggernaut", "Mrs. Wallace", "Buttercup" ] }, { "id":1, "imageLink":"https://s3.amazonaws.com/freecodecamp/grumpy-cat.jpg", "altText":"A white cat with blue eys, looking very grumpy. ", "codeNames":[ "Oscar", "Scrooge", "Tyrion" ] }, { "id":2, "imageLink":"https://s3.amazonaws.com/freecodecamp/mischievous-cat.jpg", "altText":"A ginger cat with one eye closed and mouth in a grin-like expression. Looking very mischievous. ", "codeNames":[ "The Doctor", "Loki", "Joker" ] } ] var html = ""; //iterating through all the item one by one. json.forEach(function(val) { //getting all the keys in val (current array item) var keys = Object.keys(val); //assigning HTML string to the variable html html += "<div class = 'cat'>"; //iterating through all the keys presented in val (current array item) keys.forEach(function(key) { //appending more HTML string with key and value aginst that key; html += "<strong>" + key + "</strong>: " + val[key] + "<br>"; }); //final HTML sting is appending to close the DIV element. html += "</div><br>"; }); document.body.innerHTML = html;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM