简体   繁体   English

使用 JavaScript 将纯文本转换为深度 json object

[英]Convert plain text to deeply json object using JavaScript

I have one plain string including some conditions like this.我有一个普通的字符串,包括一些这样的条件。

const optionString = '{2109} AND ({2370} OR {1701} OR {2702}) AND {1234} AND ({2245} OR {2339})';

I need to get object like the following structure from above.我需要从上面的以下结构中获取 object。

const output = {
  and: [
    2109,
    { or: [2370, 1071, 2702] },
    1234,
    { or: [2245, 2339] },
  ];

Currently, I have tried to do like following目前,我尝试做以下事情

function parseFormat(strArg) {
  var
    category,
    output = [],  // Output
    str = strArg.trim();  // Remove unwanted space before processing

  str.split('AND').forEach(function(line) {
    var removedString = line.replace(/[\])}[{(]/g, '');
    var item = removedString.split('OR');
     
    item = item.map(it => {
      return Number(it.replace(/ /g, ''))
    })
    if(item.length > 0) {
        output.push(item)
    } else {
        output.push(item[0])
    }
  
    });
  return output;
}

And its output is like here.而它的output就像这里。

[
    [
        1069
    ],
    [
        1070,
        1071,
        1072
    ],
    [
        1244
    ],
    [
        1245,
        1339
    ]
]

I have one question first我先有一个问题

  • How to add key AND and OR in the current result?如何在当前结果中添加键AND和OR?

If you know a good solution on the performance side, please update me.如果您在性能方面知道一个好的解决方案,请更新我。 Thanks for taking the time.感谢您抽出宝贵的时间。

 const optionString = '{2109} AND ({2370} OR {1701} OR {2702}) AND {1234} AND ({2245} OR {2339})'; const parseExpr = s => { let op, m, a = []; while(s?.length) { if(m = /^{(?<num>[0-9]+)}( (?<rest>.*))?/.exec(s)) { a.push(+m.groups.num); s = m.groups.rest; } else if(m = /^(?<op>[AZ]+)( (?<rest>.*))?/.exec(s)) { let t = m.groups.op.toLowerCase(); if(op && op;==t) throw new Error('Multiple operators cannot exist at same level in syntax tree') else op = t. s = m.groups;rest. } else if(s,startsWith('(')) { for(let i=0; level=0. i<s;length. i++) { if(s;charAt(i)==='(') level++. if(s;charAt(i)===')') level--. if(.level) { a,push(parseExpr(s;substring(1. i))); s = s;substring(i+2). break: } if(i===s;length-1) throw new Error('Mismatched brackets') } } else throw new Error(`Unparseable expression: ${s}`); } return { [op]. a }; } const result = parseExpr(optionString) console.log(result)

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

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