c# - How to get attribute from a deserialized nested object? -


i have json file structure:

{     "person1": [{"name": "bobby"}, {"age": 25}, {"height": 178}, {"hobby": "piano"}],     "person2": [{"name": "tyler"}, { "age": 29}, {"height": 185}, {"hobby": "basketball"}],     "person3": [{"name": "mike"}, {"age": 30}, {"height": 192}, {"hobby": "football"}] } 

after want attribute of every object in data. here code far:

        jobject json = jobject.parse(file.readalltext(*json file*));         jsonstring = json.tostring();                rootobject data = jsonconvert.deserializeobject<rootobject>(jsonstring);           //needed code here         console.writeline(*hobby of tyler*)          console.readkey();       } } //====================================json class====================================== public class person1 {     public string name { get; set; }     public int16 age { get; set; }     public int16 height { get; set; }     public string hobby { get; set; } }  public class person2 {     public string name { get; set; }     public int16 age { get; set; }     public int16 height { get; set; }     public string hobby { get; set; } }  public class person3 {     public string name { get; set; }     public int16 age { get; set; }     public int16 height { get; set; }     public string hobby { get; set; } }  public class rootobject {     public list<person1> person1 { get; set; }     public list<person2> person2 { get; set; }     public list<person3> person3 { get; set; } } } 

i appreciated if can me this. moreover, add objects property list , tie them essential well. i'm stuck moment.

example: listbox1: personid: person1, person2, person3 listbox2: name/age/height/hobby textbox3: output attribute

thank you!

update: have searching in darkness , until get

class program     {         public static string url = @"c:\users\admin\desktop\getdata3.json";         public static string jsonstring = "";         static void main(string[] args)         {             jobject json = jobject.parse(file.readalltext(url));             jsonstring = json.tostring();              //==========second method=======================================             console.writeline("==============================================================");             console.write("name: "+ person.person1[0].name);             console.write("   age: "+ person.person1[1].age);             console.write("   height: "+ person.person1[2].height);             console.writeline("   hobby: "+ person.person1[3].hobby);               console.readkey();          }     }    //class===================================     public class person     {          public string name { set; get; }          public int age { set; get; }          public int height { set; get; }           public string hobby { set; get; }     }      public class rootobject     {         public list<person> person1 { get; set; }         public list<person> person2 { get; set; }         public list<person> person3 { get; set; }      } } 

output console: name: bobby age: 25 height: 178 hobby: piano

well start seems don't understand classes , objects. don't want create new class each person new instance of the person class. example:

person person1 = new person(); person person2 = new person(); person person3 = new person(); 

this code creates 3 instances of person class. free set/get properties of each instance of person class.

note: above code uses default constructor, create own constructor assign value of persons properties @ time of instantiation.

your person class this:

public class person {    public string name { get; set; }    public int16 age { get; set; }    public int16 height { get; set; }    public string hobby { get; set; }      public override string tostring()     {         return $"name: {name}, age: {age}, height: {height}, hobby: {hobby}";     } } 

note: convention use pascalcase properties accessor methods, it's etiquette , helpful override tostring()

okay, can @ json. creating 3 json arrays, don't think want, i've changed 3 json objects:

{   "person1": {"name": "bobby", "age": 25, "height": 178, "hobby": "piano"},    "person2": {"name": "tyler", "age": 29, "height": 185, "hobby": "basketball"},    "person3": {"name": "mike", "age": 30, "height": 192, "hobby": "football"} } 

now have cleaned json can tie together:

static void main(string[] args) {      //create instance of person class   person person1 = new person();     //create json object file    jobject json = jobject.parse(file.readalltext(@"json-file-path"));    //assign instances properties using json object    person1.name = json["person1"]["name"].tostring();   person1.age =  convert.toint16(json["person1"]["age"]);   person1.height = convert.toint16(json["person1"]["height"]);   person1.hobby = json["person1"]["hobby"].tostring();    //write person object console   console.writeline(person1.tostring()); } 

when getting values jobject first must key, in case key "person1", , value assigned token using tokens name, must appropriately handle value returned token, can take many forms above achieved through simple converts , tostrings.

now can rinse , repeat creating instance of person class, assigning properties created jobject instance , using them you'd like, in case writing console.

edit use original json structure

static void main(string[] args)  {      //create instance of person class   person person1 = new person();   person person2 = new person();   person person3 = new person();    //create json object file     jobject json = jobject.parse(file.readalltext(@"json-file-path"));    //get jarray each person    jarray a1 = (jarray)json["person1"];   jarray a2 = (jarray)json["person2"];   jarray a3 = (jarray)json["person3"];    //person 1   person1.name = a1[0]["name"].tostring();   person1.age =  convert.toint16(a1[1]["age"]);   person1.height = convert.toint16(a1[2]["height"]);   person1.hobby = a1[3]["hobby"].tostring();    //person 2   person2.name = a2[0]["name"].tostring();   person2.age = convert.toint16(a2[1]["age"]);   person2.height = convert.toint16(a2[2]["height"]);   person2.hobby = a2[3]["hobby"].tostring();     //person 3    person3.name = a3[0]["name"].tostring();    person3.age = convert.toint16(a3[1]["age"]);    person3.height = convert.toint16(a3[2]["height"]);    person3.hobby = a3[3]["hobby"].tostring();      console.writeline(person1.tostring());     console.writeline(person2.tostring());     console.writeline(person3.tostring()); } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -