简体   繁体   English

对于HTML表将ndjson转换为json

[英]Convert ndjson to json for HTML table

I would like to know if it is possible to convert the ndjson data from this API: https://lichess.org/api/team/overberg-chess-group/users and turn it into an HTML table.我想知道是否可以从这个 API 转换 ndjson 数据: https://lichess.org/api/team/overberg-chess-group/users并将其转换为 HTML 表。 I have found some javascript snippets that will convert normal json into an html table but not ndjson.我发现了一些 javascript 片段,可以将正常的 json 转换为 html 表,但不能转换 ndjson。 Any help would be appreciated任何帮助,将不胜感激

Looks like the API pulls a file that's very close to JSON. Assuming that the API data is in var sourceData , just a few tweaks are required...看起来 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 + ']' );

At this point, dataObject contains the object data representing the ndjson data pulled via the API. Note that when you go to place this object into a HTML table, you'll need to convert the pipe characters "|"此时,dataObject中包含object数据,代表API拉取的ndjson数据。注意,当你go将这个object放入HTML表时,你需要将885302115|"888字符转换back into line feeds... This should help you along your way...回到换行...这应该可以帮助您前进...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM