I have an array of objects like this in json
format:
{"results":[{"SwiftCode":"","City":"","BankName":"Deutsche Bank","Bankkey":"10020030","Bankcountry":"DE"},{"SwiftCode":"","City":"10891 Berlin","BankName":"Commerzbank Berlin (West)","Bankkey":"10040000","Bankcountry":"DE"}]}
What I want to get is a object[]
in C#, where one object contains all the data what is in one json object. The thing is, I can NOT make a class with the properties of this object like here:
public class Result { public int SwiftCode { get; set; } public string City { get; set; } // . // . public string Bankcountry { get; set; } }
Because I get everytime different results back, but I know it's always an array of objects. Someone knows how I could manage to get an array of objects back?
EDIT
I have to pass this object to powershell via WriteObject(results)
. So the ouput should only be the object IN the array
.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
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; } } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With