// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Text.Json; using System.Text.Json.Serialization; namespace JsonProc.TreeCore { public class Program { private const string json = "[ { \"no\" : 1, \"name\" : \"Alan A\", \"role\" : \"Manager\" }, " + "{ \"no\" : 2, \"name\" : \"Brian B\", \"role\" : \"Engineer\" }, " + "{ \"no\" : 3, \"name\" : \"Chris C\", \"role\" : \"Sales rep\" } ]"; public static void Main(string[] args) { // iterate over all employees using(JsonDocument employees = JsonDocument.Parse(json)) { foreach(JsonElement employee in employees.RootElement.EnumerateArray()) { // find no, name and role int no = employee.GetProperty("no").GetInt32(); string name = employee.GetProperty("name").GetString(); string role = employee.GetProperty("role").GetString(); // print Console.WriteLine(no); Console.WriteLine(name); Console.WriteLine(role); } // add employee // **** not possible as JsonDocument is immutable **** // write out Console.WriteLine(employees.RootElement); } } } }