【问题标题】:html table to json - modify cellshtml表到json - 修改单元格
【发布时间】:2017-01-11 12:18:05
【问题描述】:

嗯,我有以下 html 表:

  <table id="people" border="1">
    <thead>
      <tr>
        <th>Name</th>
        <th>Places</th>
        <th>Grade</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Oscar</td>
        <td>New York</td>
        <td>16.5</td>
      </tr>
      <tr>
        <td>Antonio</td>
        <td>
          <p>New York</p>
          <p>Chicago</p>
          <p>Toronto</p>
        </td>
        <td>14</td>
      </tr>
      <tr>
        <td>Jessica</td>
        <td>New York</td>
        <td>19</td>
      </tr>
    </tbody>   

现在,当我使用 jquery tableToJson 函数时,我得到以下输出:

[
    {"Name":"Oscar","Places":"New York","Grade":"16.5"},
    {"Name":"Antonio","Places":"New York\n\t\t\tChicago\n\t\t\t\tToronto","Grade":"14"},
    {"Name":"Jessica","Places":"New York","Grade":"19"}
]

现在看看 Antonio,创建的函数

{"Name":"Antonio","Places":"New York\n\t\tChicago\n\t\t\t\tToronto","Grade":"14"}

但不是

\n

我希望地点也显示为 JSON 数组,如下所示:

{"Name":"Antonio","Places":["New York","Chicago","Toronto"],"Grade":"14"}

我知道这里描述了一些选项 (https://github.com/lightswitch05/table-to-json),但我不知道该使用哪一个,或者如何使用它们,因为它没有真正记录,如何详细使用它们。

任何帮助将不胜感激:-)

【问题讨论】:

    标签: javascript jquery html json html-table


    【解决方案1】:

    如果您想更新现有数组,请尝试使用此

    var places = [
        {"Name":"Oscar","Places":"New York","Grade":"16.5"},
        {"Name":"Antonio","Places":"New York\n Chicago\n Toronto","Grade":"14"},
        {"Name":"Jessica","Places":"New York","Grade":"19"}
    ];
    
    for(var x=0;x<places.length;x++){
      places[x].Places = places[x].Places.split("\n");
    }
    console.log(places);
      

    【讨论】:

    • 谢谢@Akshay,虽然这只是一个例子......我真正的 html 表看起来有点可怕,不同的 \n 和 \t 计数(见我的编辑)但我想我可以只需将它们也替换为 split ;)
    • 欢迎您@DavidSonnenfeld,如果您在使用它时遇到问题,请将其发布为评论,我也许可以帮助您
    猜你喜欢
    • 1970-01-01
    • 2019-09-07
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多