C # Analyse d’un tableau d’objects JSON

J’ai un tableau d’objects comme celui-ci au format json :

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

Ce que je veux obtenir est un object[] en C #, où un object contient toutes les données contenues dans un object JSON. Le problème est que je ne peux PAS créer de classe avec les propriétés de cet object comme ici:

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

Parce que je reçois à chaque fois des résultats différents, mais je sais que c’est toujours un tableau d’objects. Quelqu’un sait comment je pourrais réussir à récupérer un tableau d’objects?

MODIFIER

Je dois transmettre cet object à WriteObject(results) via WriteObject(results) . Donc, la sortie ne devrait être que l’object DANS le array .

Utilisez newtonsoft comme ceci:

 using System.Collections.Generic; using System.Linq; using Newtonsoft.Json.Linq; class Program { static void Main() { ssortingng 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; } } } } 

Utilisez la bibliothèque NewtonSoft JSON.Net .

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

J’espère que cela t’aides.

Bien que ce soit une vieille question, je pensais poster ma réponse quand même, si cela peut aider quelqu’un à l’avenir

  JArray array = JArray.Parse(jsonSsortingng); foreach (JObject obj in array.Children()) { foreach (JProperty singleProp in obj.Properties()) { ssortingng name = singleProp.Name; ssortingng value = singleProp.Value.ToSsortingng(); //Do something with name and value //System.Windows.MessageBox.Show("name is "+name+" and value is "+value); } } 

Cette solution utilise la bibliothèque Newtonsoft, n’oubliez pas d’inclure à l’ using Newtonsoft.Json.Linq;

Je viens d’avoir une solution un peu plus facile, obtenir une liste d’un object JSON. J’espère que cela peut aider.

J’ai un JSON comme ça:

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

Et fait des types comme ça

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

et la partie “magique”

  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.ToSsortingng())); } 

Je crois que c’est beaucoup plus simple.

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

ssortingng jsonData1 = @ “[{” “name” “:” “0” “,” “price” “:” “40” “,” “count” “:” “1” “,” “productId” “:” “4” “,” “catid” “:” “4” “,” “productTotal” “:” “40”, “” orderstatus “”: “” 0 “”, “” clé de commande “”: “” 123456789 “”}] “;

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

Impossible d’parsingr, si le vaule est une chaîne ..

regardez nom: repas, si nom: 1 alors il va parsingr