Skip to content

Commit 4fc0bb0

Browse files
authored
Update TXTtoJSON.cs
1 parent f77d290 commit 4fc0bb0

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

TXTtoJSON.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
1-
using System;
2-
using System.IO;
3-
using Newtonsoft.Json;
4-
using System.Collections.Generic;
1+
using System;
2+
using System.Text.Json;
53

6-
7-
namespace Test_c_
4+
public class TXTtoJSON
85
{
9-
internal class Program
6+
public static async Task<string> LoadWords()
107
{
11-
static async Task Main(string[] args)
8+
using (HttpClient client = new HttpClient())
129
{
13-
Console.WriteLine("Hello, World!");
10+
Console.WriteLine("Connecting");
11+
string content = await client.GetStringAsync("https://cheaderthecoder.github.io/5-Letter-words/words.txt");
12+
string[] lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
1413

15-
var words = await LoadWords();
14+
List<string> wordList = lines.ToList();
1615

17-
var json = JsonConvert.SerializeObject(words);
16+
// Create an anonymous object with the word list nested under a "words" key
17+
var wordObject = new { words = wordList };
1818

19-
Console.WriteLine(json);
20-
Console.ReadKey();
19+
// Convert the object to a JSON string
20+
string jsonObject = JsonSerializer.Serialize(wordObject, new JsonSerializerOptions { WriteIndented = true });
2121

22-
File.WriteAllText("words.json", json);
22+
return jsonObject;
2323
}
24+
}
2425

25-
public static async Task<List<string>> LoadWords()
26-
{
27-
using (HttpClient client = new HttpClient())
28-
{
29-
Console.WriteLine("Connecting");
30-
string content = await client.GetStringAsync("https://cheaderthecoder.github.io/5-Letter-words/words.txt");
31-
string[] lines = content.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
32-
33-
return lines.ToList();
34-
}
35-
}
26+
public static async Task Main(string[] args)
27+
{
28+
string jsonWords = await LoadWords();
29+
await File.WriteAllTextAsync("words.json", jsonWords);
30+
Console.WriteLine(jsonWords);
31+
Console.ReadKey();
3632
}
3733
}

0 commit comments

Comments
 (0)