简体   繁体   English

从文本文件值创建JSON对象

[英]Creating JSON object from text file values

I want to convert the plaintext Public Suffix List to JSON object so as to use it in my script. 我想将明文公共后缀列表转换为JSON对象,以便在我的脚本中使用它。 I just want the list of TLD's. 我只想要TLD列表。
The list is here https://publicsuffix.org/list/effective_tld_names.dat 列表在这里https://publicsuffix.org/list/effective_tld_names.dat
My initial thought was to regex match the file for suffix list but I don't know how to regex files in javascript. 我最初的想法是正则表达式匹配文件的后缀列表,但我不知道如何在JavaScript中的正则表达式文件。 I'm kind of new to javascript. 我是javascript的新手。

Anyone having idea, how to achieve this. 任何有想法的人,如何实现这一目标。 Thank You 谢谢

First off, the list doesn't seem to be allowed to do cross-domain, so direct AJAX to the resource isn't possible. 首先,列表似乎不允许跨域,因此无法直接向资源使用AJAX。

What you can do is have your server load it for you (PHP: file_get_contents , JS: http.get ). 您可以做的是让您的服务器为您加载它(PHP: file_get_contents ,JS: http.get )。 Then, implement a parser. 然后,实现一个解析器。 I'm not familiar with the format of the file, but you can read the file line by line, skip blank lines and lines with // . 我不熟悉文件的格式,但你可以逐行读取文件,跳过空行和//行。 Then load them into an array (PHP: array_push , JS: Array.prototype.push ), serialize (PHP: json_encode , JS: JSON.serialize ) and ship as JSON to your app. 然后将它们加载到一个数组(PHP: array_push ,JS: Array.prototype.push ),serialize(PHP: json_encode ,JS: JSON.serialize )并作为JSON发布到您的应用程序。

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

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