I have a JSON file with this 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 that i want to get the attribute of every object in the data. So here is my code so 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 would be really appreciated if someone can help me with this. Moreover, to add all objects property to a list and tie them is essential as well. I'm stuck the moment.
Example: ListBox1: personID: person1, person2, person3 ListBox2: name/age/height/hobby TextBox3: output attribute
Thank you!
UPDATE: I have searching in darkness and until now this is what i 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