2017-08-30 15 views
0

私は複数の行のtxtファイルに印刷したい辞書があります。複数行の文字列をブロブに印刷するには?

これを実現するために、私は:
1)JSON.stringifyを経由して文字列に辞書を変換(辞書)
2)ブロブに文字列を入力し、それを

私がいる手間を省きます出力は次のように、辞書の本当に醜い1行の結果であるということである。

{"key1":"value1","key2":"value2","key3":"value3",etc} 

しかし、私はこのようにそれをしたい:

key1:value1 
key2:value2 
key3:value3 
etc. 

どうすればいいですか?

result = JSON.stringify(dict); 

// Create blob instance and input the string 
var blob1 = new Blob([result], { type: "text/plain" }); 
url = window.URL.createObjectURL(blob1); 

// Create link to download the blob as txt file 
var a = document.createElement("a"); 
document.body.appendChild(a); 
a.style = "display: none"; 
a.href = url; 
a.setAttribute("download","My Open Tabs"); 
a.click(); 
window.URL.revokeObjectURL(url); 
+2

JSON.stringify => 'JSON.stringify(dict、null、 '');'の3番目の引数を使用します。ここを参照してくださいhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – Fefux

答えて

1

同様の効果を期待するには、JSON.stringifyに引数を追加します。

JSON.stringify({ key1: 'value1', key2: 'value2' }, null, '\n'); // Unix 
JSON.stringify({ key1: 'value1', key2: 'value2' }, null, '\r\n'); // Windows 

WindowsとUnixの違いについて言及するために、@Fefuxをありがとう!

第3引数は、目的の区切り文字を受け入れます。

+0

うーん、それは私がconsole.logそれは動作しますが、ブロブからのtxtファイルはまだそれを平らげます。 .. – tkim90

+0

私は自分の答えを更新しました。代わりに ''\ n'を使用してください。 –

+0

まだ平坦です。私はcreateObjectURL(blob1)がそれを平坦化していると思います。/ – tkim90

関連する問題