将纯文本转换为具有键值对的对象

问题描述 投票:0回答:2

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

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"
}

我正在尝试将纯文本转换为JSON。我有这样的纯文字。我确实设法将水果,国家,价格和质量转换为单行,但是我无法处理...

javascript object
2个回答
0
投票

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.