将ndjson转换为HTML表的json

问题描述

我想知道是否可以通过以下API转换ndjson数据:https://lichess.org/api/team/overberg-chess-group/users并将其转换为HTML表。我发现了一些JavaScript片段,可以将普通的json转换为html表,而不是ndjson。任何帮助将不胜感激

解决方法

好像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表时,需要转换管道字符“ |”返回换行...这应该会帮助您前进...