C# 将Json反序列化到类
如果我有一个JSON文件C# 将Json反序列化到类,c#,json,visual-studio,visual-c++,C#,Json,Visual Studio,Visual C++,如果我有一个JSON文件 { "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" }, "pho
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
,,
我想使用序列化程序。
我知道我需要创建一个适合JSON类别的类
所以我创造了这个:
class Person
{
public String firstName;
public String lastName;
public String age;
public class address
{
public String streetAddress;
public String city;
public String state;
public String postalCode;
}
public class phoneNumber
{
public String type;
public String number;
}
}
它适用于年龄和姓名,但不适用于地址和电话号码(我不知道如何在类文件中创建它们)。
我希望你能帮助我。Via
公共类地址
{
公共字符串streetAddress{get;set;}
公共字符串city{get;set;}
公共字符串状态{get;set;}
公共字符串postalCode{get;set;}
}
公共类电话号码
{
公共字符串类型{get;set;}
公共字符串编号{get;set;}
}
公共阶层人士
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共整数{get;set;}
公共广播地址{get;set;}
公共列表电话号码{get;set;}
}
您没有为地址和电话号码创建属性。必须有一个目标属性才能将数据放入该属性
大概是这样的:
class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; } // here
public IEnumerable<PhoneNumber> phoneNumber { get; set; } // and here
public class Address { /.../ }
public class PhoneNumber { /.../ }
}
班级人员
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共整数{get;set;}
公共地址{get;set;}//此处
公共IEnumerable phoneNumber{get;set;}//和此处
公共类地址{/../}
公共类电话号码{/../}
}
使用getter和setter
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; }
}
public class Address
{
public string streetAddress { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}
public class PhoneNumber
{
public string type { get; set; }
public string number { get; set; }
}
谢谢大家。现在我明白了。我所面临的问题是,我有4个JSON文件,每个文件的大小为500-800MB。我如何处理这些文件?@RonF:这听起来像是一个独立的无关问题,可能值得发表自己的堆栈溢出帖子。你会想详细说明你尝试过什么以及你面临的问题。一般来说,大文件与小文件的工作方式相同,只是需要更多的数据。谢谢。我会搜索所有答案都是一样的,但我想没关系。哈哈,我算是第一个吗?
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public Address address { get; set; }
}
public class Address
{
public string streetAddress { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postalCode { get; set; }
}
public class PhoneNumber
{
public string type { get; set; }
public string number { get; set; }
}