2016-10-24 11 views
1

私は以下のようなテキストファイルから従業員の詳細を読んでいます。私のような結果物を必要とし、この応答から今テキストファイルを読み込み、特定の形式のJSONに変換する

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. 

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

は、それは私のような応答を与える

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." 
    }] 
]; 

私はこれをどのように行うことができますか?誰も私にこれを手伝ってもらえますか?前もって感謝します。

+0

によって、我々はそれを分割することができますブロックを返す期待される結果は有効なJSONではありません。 –

+0

したがって、すべての行の後にリターン文字(\ rまたは\ n?)があり、すべてのレコードの後に​​2つの改行がありますか? – gurvinder372

+0

@Jenz - なぜ\ nや\ rの文字で表示しましたか?表示された応答ファイルの一方または両方が含まれています。 – enhzflep

答えて

1

の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>

関連する問題