C#解析JSON对象数组

我有一个像json格式的对象数组:

 {"results":[{"SwiftCode":"","City":"","BankName":"Deutsche Bank","Bankkey":"10020030","Bankcountry":"DE"},{"SwiftCode":"","City":"10891 Berlin","BankName":"Commerzbank Berlin (West)","Bankkey":"10040000","Bankcountry":"DE"}]} 

我想得到的是C#中的object[] ,其中一个对象包含一个json对象中的所有数据。 问题是,我不能像这里一样创建一个具有此对象属性的类:

 public class Result { public int SwiftCode { get; set; } public string City { get; set; } // . // . public string Bankcountry { get; set; } } 

因为我每次都得到不同的结果,但我知道它总是一个对象数组。 有人知道我怎么能设法得到一个对象数组?

编辑

我必须通过WriteObject(results)将此对象传递给powershell。 所以输出应该只是array的对象。

像这样使用newtonsoft :

 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(); 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 AllChildren(JToken json) { foreach (var c in json.Children()) { yield return c; foreach (var cc in AllChildren(c)) { yield return cc; } } } } 

使用NewtonSoft JSON.Net库。

 dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString); 

希望这可以帮助。

虽然这是一个老问题,但我认为无论如何我都会发布我的答案,如果这对未来有帮助的话

  JArray array = JArray.Parse(jsonString); foreach (JObject obj in array.Children()) { 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); } } 

这个解决方案使用Newtonsoft库,别忘了包含using Newtonsoft.Json.Linq;

我刚刚得到一个解决方案,从JSON对象中获取一个列表更容易一些。 希望这可以提供帮助。

我有一个像这样的JSON:

 {"Accounts":"[{\"bank\":\"Itau\",\"account\":\"456\",\"agency\":\"0444\",\"digit\":\"5\"}]"} 

并制作了这样的类型

  public class FinancialData { public string Accounts { get; set; } // this will store the JSON string public List AccountsList { get; set; } // this will be the actually list. } public class Accounts { public string bank { get; set; } public string account { get; set; } public string agency { get; set; } public string digit { get; set; } } 

和“魔术”部分

  Models.FinancialData financialData = (Models.FinancialData)JsonConvert.DeserializeObject(myJSON,typeof(Models.FinancialData)); var accounts = JsonConvert.DeserializeObject(financialData.Accounts) as JArray; foreach (var account in accounts) { if (financialData.AccountsList == null) { financialData.AccountsList = new List(); } financialData.AccountsList.Add(JsonConvert.DeserializeObject(account.ToString())); } 

我相信这更简单;

 dynamic obj = JObject.Parse(jsonString); string results = obj.results; foreach(string result in result.Split(')) { //Todo } 

string jsonData1 = @“[{”“name”“:”“0”“,”“price”“:”“40”“,”“count”“:”“1”“,”“productId”“:” “4””, “” CATID “”: “” 4 “”, “” productTotal “”: “” 40 “”, “” orderstatus “”: “” 0 “”, “” orderkey “”: “” 123456789 “”}]“;

  string jsonData = jsonData1.Replace("\"", ""); DataSet ds = new DataSet(); DataTable dt = new DataTable(); JArray array= JArray.Parse(jsonData); 

如果vaule是一个字符串,则无法解析..

看名字:饭菜,如果名字:1那么它会解析