JsonTextWriter writer = new JsonTextWriter(new StreamWriter("file.json")); writer.WriteStartArray(); int[] numbers = {1, 2, 3, 4, 5}; foreach (int number in numbers) { writer.WriteValue(number); } writer.WriteEndArray(); writer.Close();
JsonTextWriter writer = new JsonTextWriter(new StreamWriter("file.json")); writer.WriteStartArray(); Person[] people = {new Person() {Name = "Bob", Age = 30}, new Person() {Name = "Alice", Age = 25}}; foreach (Person person in people) { writer.WriteStartObject(); writer.WritePropertyName("Name"); writer.WriteValue(person.Name); writer.WritePropertyName("Age"); writer.WriteValue(person.Age); writer.WriteEndObject(); } writer.WriteEndArray(); writer.Close();In both examples, the WriteEndArray method is used to close the array that was opened with the WriteStartArray method. This ensures that the JSON is correctly formatted and can be deserialized correctly.