Rewrite JSON with Fetch

1.重写json请求部分:
HTML文件代码如下:
<html>
......
<script>   var myList = document.querySelector(‘ul‘);   fetch(‘https://raw.githubusercontent.com/Bulalalala-Ly/package/master/rewrite.json‘)   .then(function(response) {     if (!response.ok) {       throw new Error("HTTP error, status = " + response.status);     }     return response.json();   })   .then(function(json) {   for(var i = 0; i < json.members.length; i++) {     var listItem = document.createElement(‘li‘);     listItem.innerHTML = ‘<strong>‘ + json.members[i].Name + ‘</strong>‘;     listItem.innerHTML +=‘ age ‘ + json.members[i].Age + ‘.‘;     listItem.innerHTML +=‘ work‘ + json.members[i].Work + ‘</strong>‘;     myList.appendChild(listItem);     }   })   .catch(function(error) {   var p = document.createElement(‘p‘);   p.appendChild(     document.createTextNode(‘Error: ‘ + error.message)   );   document.body.insertBefore(p, myList);   }); </script>
</html>
2.json文件代码如下:
{ "members" : [
    {
      "Name": "liyang", 
      "Age" : 27, 
      "Work": "Refrigerated foods"
     },
    { 
      "Name": "xiaohui", 
      "Age" : 30, 
      "Work": "the Snack isle"
     },
    {
      "Name": "huanghai", 
      "Age" : 41, 
      "Work": "Refrigerated foods"
     }
  ]
posted @ 2019-01-10 21:20  Yuki-Lan  阅读(99)  评论(0编辑  收藏  举报