我有一个具有以下结构的JSON文件:
{
"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"}]
}
之后我想获取数据中每个对象的属性。所以这是我目前的代码:
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; }
}
}
如果有人可以帮助我,我将非常感激。此外,将所有对象属性添加到列表并绑定它们也是必不可少的。我陷入了困境。
实施例: ListBox1:personID:person1,person2,person3 ListBox2:名称/年龄/身高/爱好 TextBox3:输出属性
谢谢!
更新:我在黑暗中寻找,直到现在这是我得到的
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控制台:姓名:Bobby年龄:25岁身高:178爱好:钢琴
答案 0 :(得分:1)
首先,好像你不完全理解类和对象。您不想为每个人创建新课程,而是为 人员课程创建一个新实例。例如:
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
此代码创建3个person类实例。您现在可以自由设置/获取人员类的每个实例的属性。
注意:上面的代码使用默认构造函数,你可以创建自己的构造函数来在实例化时分配人员属性的值。
你的班级看起来像这样:
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}";
}
}
注意:对于属性使用PascalCase是常规,因为它们是访问器方法,它也是很好的礼仪,并且通常有助于覆盖ToString()
好的,现在我们可以查看您的JSON了。你正在创建三个JSON Arrays,我认为你不想要,所以我把它改成了三个JSON对象:
{
"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"}
}
现在我们已经清理了你的JSON,我们可以把它绑在一起:
static void Main(string[] args) {
//Create an instance of the person class
Person person1 = new Person();
//Create a json object from your file
JObject json = JObject.Parse(File.ReadAllText(@"json-file-path"));
//Assign the instances properties using the 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 the person object to the console
Console.WriteLine(person1.ToString());
}
首先从JObject获取值时必须获取密钥,在这种情况下,密钥是&#34; person1&#34;,然后通过使用令牌名称获取赋值给您的值,那么您必须妥善处理令牌返回的值,这可以采取多种形式,但通过简单的Converts和ToStrings实现。
现在你可以冲洗并重复创建person类的实例,从已经创建的JObject实例中分配它的属性并使用它们,但是在我的情况下,将它写入控制台。
编辑使用原始JSON结构
static void Main(string[] args)
{
//Create an instance of the person class
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
//Create a json object from your file
JObject json = JObject.Parse(File.ReadAllText(@"json-file-path"));
//Get the jarray for 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());
}