【问题标题】:Javascript library for convert .ini file to .json file (client side) [closed]用于将 .ini 文件转换为 .json 文件的 Javascript 库(客户端)[关闭]
【发布时间】:2014-10-19 12:11:39
【问题描述】:

我正在寻找一个 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格式

有什么想法吗?

【问题讨论】:

  • .ini文件是否也存储在本地?
  • 是的,(但可以从服务器获取文件)。我的观点是,所有的转换都将在客户端进行。
  • 这不只是一个简单的字符串操作任务吗?
  • 是的,但是如果有图书馆可以完成这项工作,为什么要这么努力......
  • 只是为了学点东西; )。请注意,搜索图书馆或其他场外资源对于 SO 来说是题外话。

标签: javascript json converter ini


【解决方案1】:

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

这是一个建议(ES6 sn-p,inifile 在 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查看它的实际应用

【讨论】:

  • @Kooilnc 你能把你的答案从,currrentBlock改成,currentBlock
  • 当然,但不再需要它:使用 ES6 将代码更新为 sn-p
  • 不错的解决方案,但 INI 文件中的 cmets 通常以 ; 开头,而不是 #。见this Wikipedia Entry。 (它说有些程序使用# 所以也许考虑添加对两者的支持)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
  • 2016-06-05
  • 2011-11-30
  • 1970-01-01
  • 2013-06-23
相关资源
最近更新 更多