C# Parsing JSON array of objects
Though this is an old question, I thought I'd post my answer anyway, if that helps someone in future
JArray array = JArray.Parse(jsonString); foreach (JObject obj in array.Children<JObject>()) { foreach (JProperty singleProp in obj.Properties()) { string name = singleProp.Name; string value = singleProp.Value.ToString(); //Do something with name and value //System.Windows.MessageBox.Show("name is "+name+" and value is "+value); } }
This solution uses Newtonsoft library, don't forget to include using Newtonsoft.Json.Linq;
Use newtonsoft like so:
using System.Collections.Generic;using System.Linq;using Newtonsoft.Json.Linq;class Program{ static void Main() { string json = "{'results':[{'SwiftCode':'','City':'','BankName':'Deutsche Bank','Bankkey':'10020030','Bankcountry':'DE'},{'SwiftCode':'','City':'10891 Berlin','BankName':'Commerzbank Berlin (West)','Bankkey':'10040000','Bankcountry':'DE'}]}"; var resultObjects = AllChildren(JObject.Parse(json)) .First(c => c.Type == JTokenType.Array && c.Path.Contains("results")) .Children<JObject>(); foreach (JObject result in resultObjects) { foreach (JProperty property in result.Properties()) { // do something with the property belonging to result } } } // recursively yield all children of json private static IEnumerable<JToken> AllChildren(JToken json) { foreach (var c in json.Children()) { yield return c; foreach (var cc in AllChildren(c)) { yield return cc; } } }}