|
1 | | -using System; |
2 | | -using System.IO; |
3 | | -using Newtonsoft.Json; |
4 | | -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.Text.Json; |
5 | 3 |
|
6 | | - |
7 | | -namespace Test_c_ |
| 4 | +public class TXTtoJSON |
8 | 5 | { |
9 | | - internal class Program |
| 6 | + public static async Task<string> LoadWords() |
10 | 7 | { |
11 | | - static async Task Main(string[] args) |
| 8 | + using (HttpClient client = new HttpClient()) |
12 | 9 | { |
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); |
14 | 13 |
|
15 | | - var words = await LoadWords(); |
| 14 | + List<string> wordList = lines.ToList(); |
16 | 15 |
|
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 }; |
18 | 18 |
|
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 }); |
21 | 21 |
|
22 | | - File.WriteAllText("words.json", json); |
| 22 | + return jsonObject; |
23 | 23 | } |
| 24 | + } |
24 | 25 |
|
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(); |
36 | 32 | } |
37 | 33 | } |
0 commit comments