dongpinyao2203 2019-03-25 12:08
浏览 113
已采纳

如何将AJAX检索到的JSON格式的字符串转换为格式化的表格

I am new to web programming and have been having a lot of trouble with using AJAX to parse as a string and create a table.

[{"source":"1","name":"random","amount":"5"},{"source":"1","name":"random","amount":"5"}]

This is how the data is parsed as I used the code from W3SCHOOLS but adapted it to my situation I used the JSON stringify command to change it to a string also. https://www.w3schools.com/js/js_json_php.asp Based on the data above which has been retrieved by a AJAX code I am having troubles creating a table based on those results.

  • 写回答

4条回答 默认 最新

  • dsbfbz75185 2019-03-25 12:23
    关注

    If you want your myObj in a format like table, then there is nothing that automatically converts from an object to table format built into html/javascript.

    You could use some lib's to do this, but for something simple the below might be a start for you.

    const myObj = [{"source":"1","name":"random","amount":"5"},{"source":"1","name":"random","amount":"5"}];
    
    const tbody = document.querySelector("tbody");
    
    myObj.forEach(r => {
      const tr = document.createElement("tr"); 
      ["source", "name", "amount"].forEach(f => {
        const td = document.createElement("td");
        td.innerText = r[f];
        tr.appendChild(td);
      });
      tbody.appendChild(tr);
    });
    <table border="1">
      <thead>
        <th>Source</th>
        <th>Name</th>
        <th>Amount</th>
      </thead>
      <tbody>
      </tbody>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程