|
| 1 | +using System; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Serialization; |
| 4 | + |
| 5 | +namespace test |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + cell[] board2 = new cell[256]; |
| 12 | + |
| 13 | + for (int i = 0; i < board2.Length; ++i) |
| 14 | + { |
| 15 | + board2[i] = new cell(); |
| 16 | + board2[i].life = 0; |
| 17 | + board2[i].color = "#FF0000"; |
| 18 | + } |
| 19 | + /* |
| 20 | + foreach (cell i in board2) |
| 21 | + { |
| 22 | + Console.WriteLine(i.life); |
| 23 | + Console.WriteLine(i.color); |
| 24 | + } |
| 25 | + */ |
| 26 | + |
| 27 | + board2[245].life = 1; |
| 28 | + |
| 29 | + string jsonString; |
| 30 | + jsonString = JsonSerializer.Serialize(board2); |
| 31 | + Console.WriteLine(jsonString); |
| 32 | + |
| 33 | + |
| 34 | + cell[] board = new cell[256]; |
| 35 | + |
| 36 | + for (int i = 0; i < board2.Length; ++i) |
| 37 | + { |
| 38 | + board[i] = new cell(); |
| 39 | + } |
| 40 | + |
| 41 | + int count = 0; |
| 42 | + |
| 43 | + using (JsonDocument document = JsonDocument.Parse(jsonString)) |
| 44 | + { |
| 45 | + JsonElement root = document.RootElement; |
| 46 | + //JsonElement studentsElement = root.GetProperty("Students"); |
| 47 | + foreach (JsonElement cell in root.EnumerateArray()) |
| 48 | + { |
| 49 | + cell.TryGetProperty("life", out JsonElement lifeElement); |
| 50 | + board[count].life = lifeElement.GetInt32(); |
| 51 | + //Console.WriteLine(lifeElement.ToString()); |
| 52 | + cell.TryGetProperty("color", out JsonElement colorElement); |
| 53 | + board[count].color = colorElement.ToString(); |
| 54 | + //Console.WriteLine(colorElement.ToString()); |
| 55 | + count++; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + foreach (cell i in board) |
| 60 | + { |
| 61 | + Console.WriteLine(i.life); |
| 62 | + Console.WriteLine(i.color); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + } |
| 67 | + } |
| 68 | + class cell |
| 69 | + { |
| 70 | + public int life { get; set; } |
| 71 | + public string color { get; set; } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +} |
0 commit comments