用于将 .ini 文件转换为 .json 文件的 Javascript 库(客户端)

标签 javascript json converters ini

我正在尝试找到一个 JS 库,用于将 .ini 文件转换为 .json 文件

我需要所有操作都在客户端(!!!),所以 https://www.npmjs.org/package/clonkrefiniparser对我没有帮助。

例如:

.ini 文件:

[Master_Settings:1]
Model Name=RC-74DL
IP Address=192.168.1.39
Port=50000
[Device_Ports:1]
[Slave_Settings:2]
ConfigurationfilePath=C:\Users\name\Documents\K-Cssig2\Devices\RC-63D.xml
Button Label1=
[Device_Ports:2]
ADIO Mode 1 = DI
ADIO Mode 2 = DI
[Slave_Settings:11]
Model Name = Test 3
Desription=
ConfigurationfilePath=Devices\Test 3.xml
Button Label1=
Button Label2=
[Device_Ports:11]
ADIO Mode 1 = DI
ADIO Mode 2 = DI

[Serial:1:6]
Main Display=True
Default Port Description=MX660
User Port Description=MX660
Driver Name=BenQ MX660 A
Device_On_Command=N/A
Device_Off_Command=N/A
IsPowerQuery=False
IsLampQuery=False

转为.json格式

有什么想法吗?

最佳答案

这确实只是字符串操作,您实际上并不需要一个库。

这里有一个建议(ES6 代码片段,ini 文件位于 html 中的隐藏 div 内):

( () => {
    let ini2Obj = {};
    const keyValuePair = kvStr => {
    	const kvPair = kvStr.split('=').map( val => val.trim() );
        return { key: kvPair[0], value: kvPair[1] };
    };
    const result = document.querySelector("#results");
    document.querySelector( '#inifile' ).textContent
    	.split( /\n/ )                                       // split lines
        .map( line => line.replace( /^\s+|\r/g, "" ) )     // cleanup whitespace
        .forEach( line =>  {                               // convert to object
            line = line.trim();
            if ( line.startsWith('#') || line.startsWith(';') ) { return false; }
            if ( line.length ) {
              if ( /^\[/.test(line) ) {
                this.currentKey = line.replace(/\[|\]/g,'');
                ini2Obj[this.currentKey] = {};
              } else if ( this.currentKey.length ) {
                const kvPair = keyValuePair(line);
                ini2Obj[this.currentKey][kvPair.key] = kvPair.value;
              }
            } 
          }, {currentKey: ''} );
    
    result.textContent += 
    	`**Check: ini2Obj['Slave_Settings:11'].ConfigurationfilePath = ${
          ini2Obj['Slave_Settings:11'].ConfigurationfilePath}`;
    
    result.textContent += 
      `\n\n**The converted object (JSON-stringified)\n${
      JSON.stringify(ini2Obj, null, ' ')}`;
})();
.hidden {display: none}
<div class="hidden" id="inifile">
    # this is a comment line
    [Master_Settings:1]
    Model Name=RC-74DL
    IP Address=192.168.1.39
    Port=50000
    
    [Device_Ports:1]
    
    [Slave_Settings:2]
    ConfigurationfilePath=C:\Users\name\Documents\K-Cssig2\Devices\RC-63D.xml
    Button Label1=
    
    ; this is a comment line too
    [Device_Ports:2]
    ADIO Mode 1 = DI
    ADIO Mode 2 = DI
    
    [Slave_Settings:11]
    Model Name = Test 3
    Desription=
    ConfigurationfilePath=Devices\Test 3.xml
    # Note: no labels here
    Button Label1=
    Button Label2=
    
    [Device_Ports:11]
    ADIO Mode 1 = DI
    ADIO Mode 2 = DI
    
    [Serial:1:6]
    Main Display=True
    Default Port Description=MX660
    User Port Description=MX660
  
    # Note: empty lines are ok
    Driver Name=BenQ MX660 A
    Device_On_Command=N/A
    Device_Off_Command=N/A
    IsPowerQuery=False
    IsLampQuery=False
</div>

<pre id="results"></pre>

查看 this jsFiddle 中的实际操作

关于用于将 .ini 文件转换为 .json 文件的 Javascript 库(客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26450359/

相关文章:

javascript - 如何在javascript中将字母转换为数字?

c# - 如何在 XAML 中使用 String 以外的类型设置自定义属性值 [Xamarin.Forms]

php - CodeIgniter所有页面都重定向到主页吗?

javascript - 从ajax错误处理中获取单独的字符串

java - WebResource put 去掉requestEntity的 '+'字符

json - 无法将 API-Response 转换为 JSON,但可以将其打印为字符串

javascript - 我无法使用 .innerHTML 更改 HTML。不断收到消息 "cannot set innerHTML property of null"

javascript - Fancybox: 无法加载请求的内容。请稍后再试

python - 如何在 Django 中序列化 RawQuerySet?

java - JSON 转换 Map 与整数键