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

Sha*_*tin 100 c# json code-generation

给定以下JSON对象,

form = {
  "name": "",
  "address": {
    "street": "",
    "city": "",
    "province": "",
    "postalCode": "",
    "country": ""
  },
  "phoneDay": "",
  "phoneCell": "",
  "businessName": "",
  "website": "",
  "email": ""
}
Run Code Online (Sandbox Code Playgroud)

什么是自动生成以下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; }
}
Run Code Online (Sandbox Code Playgroud)

我们已经看过这些问题了:

从JSON Schema生成C#类询问JSON Schema,这可能是一种在未来使用的方法.

为Json对象生成的C#类的优点和缺点

Sha*_*tin 131

三种选择:

优点和缺点:

  • “jsonutils”非常酷,它也可以生成“Json 属性”之类的属性,http://jsonutils.com/ (2认同)

Mrc*_*ief 100

Visual Studio 2012(安装了ASP.NET和Web Tools 2012.2 RC)本支持此功能.

Visual Studio 2013以上版本具有此内置功能.

Visual Studio将JSON粘贴为Classes截图 (图片提供:robert.muehsig)

  • 我认为这个解决方案是最好的,因为你不需要外部程序! (4认同)

Ken*_*eth 9

如果将Web Essentials安装到Visual Studio中,则可以转到Edit => Past special =>将JSON粘贴为类.

这可能是最简单的.

Web Essentials:http://vswebessentials.com/