【问题标题】:Convert ndjson to json for HTML table将 ndjson 转换为 json 用于 HTML 表
【发布时间】:2020-11-11 06:41:44
【问题描述】:

我想知道是否可以从这个 API 转换 ndjson 数据:https://lichess.org/api/team/overberg-chess-group/users 并将其转换为 HTML 表格。我发现了一些 javascript sn-ps 可以将普通的 json 转换为 html 表,但不能转换为 ndjson。任何帮助将不胜感激

【问题讨论】:

    标签: javascript html json api ndjson


    【解决方案1】:

    看起来 API 提取了一个非常接近 JSON 的文件。假设 API 数据在 var sourceData 中,只需进行一些调整...

    // Add commas between the list of objects...
    data = sourceData.split( '}\n{' ).join( '},{' );
    
    // ...remove all carriage return line feeds...
    data = data.split( '\r\n' ).join( '|' );
    
    // ...and there's one instance of a double quoted string, 
    // eg, "bio":""I am committed..."".  This appears to be
    // the only occurrence of this anomaly, so the following
    // currently works, but you might run into issues in the
    // future, for example, if an attribute is a null string "".
    data = data.split( '""' ).join( '"' );
    
    let dataObject = JSON.parse( '[' + data + ']' );
    

    此时,dataObject 包含了表示通过 API 拉取的 ndjson 数据的对象数据。请注意,当您将此对象放入 HTML 表格时,您需要将管道字符“|”转换为回到换行符...这应该对您有所帮助...

    【讨论】:

      猜你喜欢
      • 2013-09-03
      • 2014-02-23
      • 2020-11-23
      • 2015-09-02
      • 2013-07-02
      • 2021-07-05
      • 2020-05-09
      • 2019-05-07
      相关资源
      最近更新 更多