如何从JSON字符串自动生成C#类文件 [英] How to auto-generate a C# class file from a JSON string
问题描述
给出以下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; }
}
我们已经研究了以下问题:
从JSON模式生成C#类正在询问有关JSON模式的问题这可能是在未来使用的一种方法.
四个选项:
-
无需安装任何内容即可使用免费的 json2csharp 网络工具.
-
如果您在Visual Studio中具有 Web Essentials ,请使用编辑">选择性粘贴">将JSON粘贴为类" .
-
使用免费的 jsonclassgenerator .exe
-
网络工具 app.quicktype.io (在 json2csharp 的链接中可用)也不需要安装任何内容.
优点和缺点:
-
jsonclassgenerator 转换为PascalCase,而其他的则没有. p>
-
app.quicktype.io 具有一些逻辑识别字典并处理名称为无效c#标识符的JSON属性.
Given the following JSON object,
form = {
"name": "",
"address": {
"street": "",
"city": "",
"province": "",
"postalCode": "",
"country": ""
},
"phoneDay": "",
"phoneCell": "",
"businessName": "",
"website": "",
"email": ""
}
what is a tool to auto-generate the following C# class?
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; }
}
We have already looked at these questions:
Generate C# classes from JSON Schema Is asking about JSON Schemas which may be an approach to use down the road.
Benefits and drawbacks of generated C# classes for Json objects
Four options:
Use the free json2csharp web tool without installing anything.
If you have Web Essentials in Visual Studio, use Edit > Paste special > paste JSON as class.
Use the free jsonclassgenerator.exe
The web tool app.quicktype.io (available in a link from json2csharp) also does not require installing anything.
Pros and Cons:
jsonclassgenerator converts to PascalCase but the others do not.
app.quicktype.io has some logic to recognize dictionaries and handle JSON properties whose names are invalid c# identifiers.
这篇关于如何从JSON字符串自动生成C#类文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!