读取文本文件并将其转换为特定格式的JSON
Read a text file and convert to JSON in a particular format
我正在从如下所示的文本文件中读取员工详细信息。
$( 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>
相关文章:
- 如何使用javascript或html下载PDF格式的填写表单
- 货币代码为欧元-金额的格式不应包含小数
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 更改angularjs中的日期-时间格式
- 将日期时间从json转换为可读格式
- NodeJS日期格式不起作用
- 我能得到正确的格式吗
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 将日期和时间转换为UTC格式的日期-Javascript
- Javascript格式UTC日期
- 元素的内容必须由格式正确的字符数据或标记组成
- Datetime格式为Friendly Time.Moment JS输出错误
- 将客户端特定的日期格式返回到服务器MVC4
- 将数字四舍五入到小数点后两位,以设置货币格式
- javascript/jquery将utc转换为短格式的本地时间
- JavaScript生成html格式的密码
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- 如何通过数据分组将json数据转换为嵌套的json数据格式