给定以下 JSON 对象,
form = {
"name": "",
"address": {
"street": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
},
"phoneDay": "",
"phoneCell": "",
"businessName": "",
"website": "",
"email": ""
}
什么是自动生成以下 C# 类的工具?
public class ContactInfo
{
public string Name { get; set; }
public Address Address { get; set; }
public string PhoneDay { get; set; }
public string PhoneCell { get; set; }
public string BusinessName { get; set; }
public string Website { get; set; }
public string Email { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
我们已经研究过这些问题:
Generate C# classes from JSON Schema正在询问 JSON 模式,这可能是一种在未来使用的方法。
Benefits and drawbacks of generated C# classes for Json objects
最佳答案
五个选项:
使用免费的jsonutils网络工具,无需安装任何东西。
如果您有 Web Essentials在 Visual Studio 中,使用 Edit > Paste special > paste JSON as class。
使用免费的jsonclassgenerator .exe
网络工具 app.quicktype.io不需要安装任何东西。
网络工具 json2csharp也不需要安装任何东西。
优点和缺点:
jsonclassgenerator转换为 PascalCase,但其他不转换。
app.quicktype.io有一些逻辑可以识别字典并处理名称为无效 c# 标识符的 JSON 属性。
关于c# - 如何从 JSON 字符串自动生成 C# 类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611674/