读取文本文件并将其转换为特定格式的JSON

Read a text file and convert to JSON in a particular format

本文关键字:定格 格式 JSON 转换 文件 取文本 读取      更新时间:2023-09-26

我正在从如下所示的文本文件中读取员工详细信息。

$( document ).ready(function() {
    $.ajax({
        url: "employees.txt",
        success:function(response) {
            console.log(response);
        }
    });
});

它给了我这样的响应:

    Mark
    Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good public speaker, and have a good sense of humor.
    Jennifer
    Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared with co-workers. She is dedicated, outgoing, and a team player.

现在从这个响应中,我需要一个结果结构,如:

 var employees = [
   ["mark", {
      "name": "Mark",
      "description": "Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good public speaker, and have a good sense of humor."
   }],
   ["jennifer", {
      "name": "Jennifer",
      "description": "Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared with co-workers. She is dedicated, outgoing, and a team player."
   }]
];

我该怎么做?有人能帮我做这件事吗?

这个用div测试的例子在HTML中有你的文本当我们得到innerText时它返回块我们可以用'n'n分割它

//split using 'n'n
function toJson(str) {
  var tt = [];
  var rw = str.split("'n'n");
  for (var i = 0; i < rw.length; i++) {
    var name = rw[i].split("'n")[0].trim();
    var description = rw[i].split("'n")[1].trim();
    var jsn = [
      name, {
        "name": name,
        "description": description
      }
    ]
    tt.push(jsn);
  }
  return tt;
}
var employees = toJson(document.getElementById("txt").innerText);
console.log(employees);
<div id='txt'>
  Mark
  <br/>Mark have 10 years of experience working with youth agencies. Mark have a bachelor’s degree in outdoor education. He raise money, train leaders, and organize units. He have raised over $100,000 each of the last six years. He consider himself a good
  public speaker, and have a good sense of humor.
  <br/>
  <br/>Jennifer
  <br/>Jennifer enjoy meeting new people and finding ways to help them have an uplifting experience. She have had a variety of customer service opportunities, through which she was able to have fewer returned products and increased repeat customers, when compared
  with co-workers. She is dedicated, outgoing, and a team player.
</div>