Good day!

The question is this!
I am trying to write all the elements of a collection to a json file, but for some reason only the last element of the collection is written to a file!

Here is the code I use to output to the console and write them to a file:

foreach (var m in listHashRepo) { Console.ForegroundColor = System.ConsoleColor.Cyan; Console.WriteLine(m); JObject wcomm = new JObject(new JProperty("url", m)); File.WriteAllText("files.json", wcomm.ToString()); } 

Only the last element is recorded in the file:

 {"url": "e92bc448678ef499cfeabb3df93ea7adad47f747"} 

I wanted him to record everything. Like that:

 {"url": "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345"} {"url": "89fsd898c23993d9571cmvfnjh450cnklfhijrwi"} {"url": "43j24j5390fsnto0g775jli43omh5oh632ji5p5p"} {"url": "e92bc448678ef499cfeabb3df93ea7adad47f747"} 

Help me figure out what could be the problem?

  • Not surprisingly, you rewrite the file again at each iteration. Please place your code not as an image, but as text, and give an example of what output file format you want. - VladD
  • @VladD Corrected the issue! - Vladislavs Geidans
  • But what you want is not JSON! JSON format would look something like this: [ {"url": "24gfd5ff7f9fd5gs98uf349k31u6g2134io6h345"}, {"url": "89fsd898c23993d9571cmvfnjh450cnklfhijrwi"} ] . - VladD
  • @VladD Yes, I did not put the brackets, I apologize. - Vladislavs Geidans

2 answers 2

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

Some analysis is needed, distinguishing the record of the first object and the subsequent ones.

 File.Delete("files.json"); foreach (var m in listHashRepo) { Console.ForegroundColor = System.ConsoleColor.Cyan; Console.WriteLine(m); JObject wcomm = new JObject(new JProperty("url", m)); if (!File.Exists("files.json")) File.WriteAllText("files.json", "[" + wcomm.ToString()); else File.AppendAllText("files.json", "," + wcomm.ToString()); } File.AppendAllText("files.json", "]");