c# - 如何从 JSON 字符串自动生成 C# 类文件

给定以下 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 属性。

https://stackoverflow.com/questions/21611674/

相关文章:

jquery - 如何检查值是否为 JSON 对象?

.net - 确保 .NET 中的 json 键是小写的

javascript - 如何通过 JavaScript for 循环创建 json?

json - pinterest api 文档

python - JSON到 Pandas 数据框

javascript - 嵌套的 JSON 对象 - 我必须对所有内容都使用数组吗?

javascript - 将 Javascript 对象编码为 Json 字符串

json - 如何使用 jq 合并来自 2 个文件的 2 个 JSON 对象?

javascript - 将json数据转换为html表

json - 在 JSON 结果中定义函数是否有效?