In fact, to serialize a collection, you can simply write
File.WriteAllText("files.json", hashes, Formatting.Indented);
( not in a loop!) This will produce this JSON:
[ "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345", "89fsd898c23993d9571cmvfnjh450cnklfhijrwi", "43j24j5390fsnto0g775jli43omh5oh632ji5p5p", "e92bc448678ef499cfeabb3df93ea7adad47f747" ]
If you also need a "url" , it becomes a little more complicated:
File.WriteAllText( "files.json", JsonConvert.SerializeObject( hashes.Select(hash => new { url = hash }), Formatting.Indented));
The result will be:
[ { "url": "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345" }, { "url": "89fsd898c23993d9571cmvfnjh450cnklfhijrwi" }, { "url": "43j24j5390fsnto0g775jli43omh5oh632ji5p5p" }, { "url": "e92bc448678ef499cfeabb3df93ea7adad47f747" } ]
From the comments: if you actually have two sequences, you can combine them with Zip :
var hashes = new[] { "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345", "89fsd898c23993d9571cmvfnjh450cnklfhijrwi", "43j24j5390fsnto0g775jli43omh5oh632ji5p5p", "e92bc448678ef499cfeabb3df93ea7adad47f747" }; var urls = new[] { "https://github.com/VladislavsGeidans/wikicar.git", "https://github.com/VladislavsGeidans/1.git", "https://github.com/VladislavsGeidans/2.git", "https://github.com/VladislavsGeidans/3.git", }; File.WriteAllText( "files.json", JsonConvert.SerializeObject( hashes.Zip(urls, (hash, url) => new { hash, url }), Formatting.Indented));
Result:
[ { "hash": "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345", "url": "https://github.com/VladislavsGeidans/wikicar.git" }, { "hash": "89fsd898c23993d9571cmvfnjh450cnklfhijrwi", "url": "https://github.com/VladislavsGeidans/1.git" }, { "hash": "43j24j5390fsnto0g775jli43omh5oh632ji5p5p", "url": "https://github.com/VladislavsGeidans/2.git" }, { "hash": "e92bc448678ef499cfeabb3df93ea7adad47f747", "url": "https://github.com/VladislavsGeidans/3.git" } ]
[ {"url": "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345"}, {"url": "89fsd898c23993d9571cmvfnjh450cnklfhijrwi"} ]. - VladD