使用键值对将纯文本转换为对象

时间:2019-11-09 21:20:09

标签: javascript object

我正在尝试将纯文本转换为JSON。我有这样的纯文字。我确实设法将水果,国家/地区,价格和质量转换为单行,但是我无法使用Description进行转换。 如何在单个key:value对中放置Description ???

Fruit: Apple
Country: Germany
Description: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, 
 .
 Some other countries where this is found:
  * India
  * Austria
  * Jakarta
  * Sample
 .
 Lorem Ipsum is simply dummy text of the printing and typesetting industry, lorem Ipsum has been the industry's standard dummy text ever.
Price: 1500$
Quality: Excellent

我希望结果是

{
Fruit: "Apple", 
Country: "Germany", 
Description: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, 
 .
 Some other countries where this is found:
  * India
  * Austria
  * Jakarta
  * Sample
 .
 Lorem Ipsum is simply dummy text of the printing and typesetting industry, lorem Ipsum has been the industry's standard dummy text ever.`,
 Price: "1500$",
 Quality: "Excellent"
}

2 个答案:

答案 0 :(得分:0)

如示例中所示,如果连续行确实以前导空格(缩进)为特征,则可以使用如下正则表达式:

function toObject(text) {
    let obj = {};
    for (let [, key, val] of text.matchAll(/(\S.*?): ([^]*?)(?=$|\n\S)/g)) {
        obj[key] = val.replace(/\n /g, "\n"); // remove the indent
    }
    return obj;
}

// Example:
let text = `Fruit: Apple
Country: Germany
Description: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, 
 .
 Some other countries where this is found:
  * India
  * Austria
  * Jakarta
  * Sample
 .
 Lorem Ipsum is simply dummy text of the printing and typesetting industry, lorem Ipsum has been the industry's standard dummy text ever.
Price: 1500$
Quality: Excellent`;

console.log(toObject(text));

这将产生一个JavaScript对象。您可以在该对象上调用JSON.stringify以获取JSON输出。

答案 1 :(得分:0)

利用多行字符串值的第二行和其他行以空格开头:

代码(https://jsfiddle.net/koldev/toxz4y6a):

function join(key, value) {
    if (key === "") {
        return;
    }
    let line = "    \"" + key + "\": ";
    value = value.slice(0, -1);
    if (value.includes("\n")) {
        line += "`" + value + "`";
    } else {
        line += "\"" + value + "\"";
    }
    return line;
}

function parse(input) {
    let lines = input.split("\n");
    let key = "";
    let value = "";
    let output = "{\n";
    for (let i = 0; i < lines.length; i += 1) {
        let line = lines[i];
        if (line.startsWith(" ")) {
            value += line + "\n";
        } else {
            let keyValue = join(key, value);
            if (keyValue) {
                output += keyValue + ",\n";
            }
            let tokens = line.split(": ");
            key = tokens[0];
            value = tokens[1] + "\n";
        }
    }
    let keyValue = join(key, value);
    if (keyValue) {
        output += keyValue + "\n";
    }
    output += "}\n";
    return output;
}

let input = document.getElementById("input").innerText;
document.getElementById("output").innerText = parse(input);

输出:

{
    "Fruit": "Apple",
    "Country": "Germany",
    "Description": `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, 
 .
 Some other countries where this is found:
  * India
  * Austria
  * Jakarta
  * Sample
 .
 Lorem Ipsum is simply dummy text of the printing and typesetting industry, lorem Ipsum has been the industry's standard dummy text ever.`,
    "Price": "1500$",
    "Quality": "Excellent"
}