// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace JsonProc.TreeNewton { 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 JArray employees = (JArray)JsonConvert.DeserializeObject(json); foreach(JObject employee in employees) { // find no, name and role int no = (int)employee["no"]; string name = (string)employee["name"]; string role = (string)employee["role"]; // print Console.WriteLine(no); Console.WriteLine(name); Console.WriteLine(role); } // add employee JObject newemployee = new JObject(); newemployee["no"] = 4; newemployee["name"] = "Dave D"; newemployee["role"] = "Intern"; employees.Add(newemployee); // write out Console.WriteLine(JsonConvert.SerializeObject(employees)); } } }