visual studio使用本事:快速天生Json、XML对应类

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
visual studio快速天生Json、XML对应类

在项目中常常用到json大概xml作为设置文件,举行序列化和反序列化就必要有对应的类,重新写一遍类就比力贫苦,这里就讲一下通过visual studio快速天生json大概xml对应范例的方法。
自动天生Json类

复制一个json文件,自动在visual studio中天生
复制Json文件
  1. {
  2.   "DeviceLinks": [
  3.     {
  4.       "UID": "device01",
  5.       "Ip": "127.0.0.1",
  6.       "Port": 502,
  7.       "SlaveId": 1,
  8.       "AcqTimeSpan": 1
  9.     },
  10.     {
  11.       "UID": "device02",
  12.       "Ip": "127.0.0.1",
  13.       "Port": 503,
  14.       "SlaveId": 1,
  15.       "AcqTimeSpan": 1
  16.     }
  17.   ],
  18.   "MqttConfig": {
  19.     "Ip": "127.0.0.1",
  20.     "Port": 1883,
  21.     "Username": "admin",
  22.     "Password": "12345"
  23.   },
  24.   "ServiceConfig": {
  25.     "PushTimeSpan": 5,
  26.     "IsPushScheduled": true,
  27.     "IsPushChanged": true
  28.   }
  29. }
复制代码
切换到一个cs的类文件

必须在c#的cs文件中,才华看到编辑中对应的菜单

选择性粘贴

在菜单栏,“编辑”→“选择性粘贴”→将JSON粘贴为类

代码自动天生,会天生一个Rootobject为json的主类,子类也会一起天生,这里根据json字段界说,自动天生对应的子类
  1. public class Rootobject
  2. {
  3.     public Devicelink[] DeviceLinks { get; set; }
  4.     public Mqttconfig MqttConfig { get; set; }
  5.     public Serviceconfig ServiceConfig { get; set; }
  6. }
  7. public class Mqttconfig
  8. {
  9.     public string Ip { get; set; }
  10.     public int Port { get; set; }
  11.     public string Username { get; set; }
  12.     public string Password { get; set; }
  13. }
  14. public class Serviceconfig
  15. {
  16.     public int PushTimeSpan { get; set; }
  17.     public bool IsPushScheduled { get; set; }
  18.     public bool IsPushChanged { get; set; }
  19. }
  20. public class Devicelink
  21. {
  22.     public string UID { get; set; }
  23.     public string Ip { get; set; }
  24.     public int Port { get; set; }
  25.     public int SlaveId { get; set; }
  26.     public int AcqTimeSpan { get; set; }
  27. }
复制代码
注意:转换巨细写,会跟随json文件,必要自己调解一下

自动天生XML类

步调一样,选择性粘贴中选择“将XML粘贴为类”

示例XML:
  1. <Configuration>
  2.   <DeviceLinks>
  3.     <DeviceLink>
  4.       <UID>device01</UID>
  5.       <Ip>127.0.0.1</Ip>
  6.       <Port>502</Port>
  7.       <SlaveId>1</SlaveId>
  8.       <AcqTimeSpan>1</AcqTimeSpan>
  9.     </DeviceLink>
  10.     <DeviceLink>
  11.       <UID>device02</UID>
  12.       <Ip>127.0.0.1</Ip>
  13.       <Port>503</Port>
  14.       <SlaveId>1</SlaveId>
  15.       <AcqTimeSpan>1</AcqTimeSpan>
  16.     </DeviceLink>
  17.   </DeviceLinks>
  18.   
  19.   <MqttConfig>
  20.     <Ip>127.0.0.1</Ip>
  21.     <Port>1883</Port>
  22.     <Username>admin</Username>
  23.     <Password>12345</Password>
  24.   </MqttConfig>
  25.   
  26.   <ServiceConfig>
  27.     <PushTimeSpan>5</PushTimeSpan>
  28.     <IsPushScheduled>true</IsPushScheduled>
  29.     <IsPushChanged>true</IsPushChanged>
  30.   </ServiceConfig>
  31. </Configuration>
复制代码
天生效果

  1. // 注意: 生成的代码可能至少需要 .NET Framework 4.5 或 .NET Core/Standard 2.0。
  2. /// <remarks/>
  3. [System.SerializableAttribute()]
  4. [System.ComponentModel.DesignerCategoryAttribute("code")]
  5. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  6. [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  7. public partial class Configuration
  8. {
  9.     private ConfigurationDeviceLink[] deviceLinksField;
  10.     private ConfigurationMqttConfig mqttConfigField;
  11.     private ConfigurationServiceConfig serviceConfigField;
  12.     /// <remarks/>
  13.     [System.Xml.Serialization.XmlArrayItemAttribute("DeviceLink", IsNullable = false)]
  14.     public ConfigurationDeviceLink[] DeviceLinks
  15.     {
  16.         get
  17.         {
  18.             return this.deviceLinksField;
  19.         }
  20.         set
  21.         {
  22.             this.deviceLinksField = value;
  23.         }
  24.     }
  25.     /// <remarks/>
  26.     public ConfigurationMqttConfig MqttConfig
  27.     {
  28.         get
  29.         {
  30.             return this.mqttConfigField;
  31.         }
  32.         set
  33.         {
  34.             this.mqttConfigField = value;
  35.         }
  36.     }
  37.     /// <remarks/>
  38.     public ConfigurationServiceConfig ServiceConfig
  39.     {
  40.         get
  41.         {
  42.             return this.serviceConfigField;
  43.         }
  44.         set
  45.         {
  46.             this.serviceConfigField = value;
  47.         }
  48.     }
  49. }
  50. /// <remarks/>
  51. [System.SerializableAttribute()]
  52. [System.ComponentModel.DesignerCategoryAttribute("code")]
  53. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  54. public partial class ConfigurationDeviceLink
  55. {
  56.     private string uIDField;
  57.     private string ipField;
  58.     private ushort portField;
  59.     private byte slaveIdField;
  60.     private byte acqTimeSpanField;
  61.     /// <remarks/>
  62.     public string UID
  63.     {
  64.         get
  65.         {
  66.             return this.uIDField;
  67.         }
  68.         set
  69.         {
  70.             this.uIDField = value;
  71.         }
  72.     }
  73.     /// <remarks/>
  74.     public string Ip
  75.     {
  76.         get
  77.         {
  78.             return this.ipField;
  79.         }
  80.         set
  81.         {
  82.             this.ipField = value;
  83.         }
  84.     }
  85.     /// <remarks/>
  86.     public ushort Port
  87.     {
  88.         get
  89.         {
  90.             return this.portField;
  91.         }
  92.         set
  93.         {
  94.             this.portField = value;
  95.         }
  96.     }
  97.     /// <remarks/>
  98.     public byte SlaveId
  99.     {
  100.         get
  101.         {
  102.             return this.slaveIdField;
  103.         }
  104.         set
  105.         {
  106.             this.slaveIdField = value;
  107.         }
  108.     }
  109.     /// <remarks/>
  110.     public byte AcqTimeSpan
  111.     {
  112.         get
  113.         {
  114.             return this.acqTimeSpanField;
  115.         }
  116.         set
  117.         {
  118.             this.acqTimeSpanField = value;
  119.         }
  120.     }
  121. }
  122. /// <remarks/>
  123. [System.SerializableAttribute()]
  124. [System.ComponentModel.DesignerCategoryAttribute("code")]
  125. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  126. public partial class ConfigurationMqttConfig
  127. {
  128.     private string ipField;
  129.     private ushort portField;
  130.     private string usernameField;
  131.     private ushort passwordField;
  132.     /// <remarks/>
  133.     public string Ip
  134.     {
  135.         get
  136.         {
  137.             return this.ipField;
  138.         }
  139.         set
  140.         {
  141.             this.ipField = value;
  142.         }
  143.     }
  144.     /// <remarks/>
  145.     public ushort Port
  146.     {
  147.         get
  148.         {
  149.             return this.portField;
  150.         }
  151.         set
  152.         {
  153.             this.portField = value;
  154.         }
  155.     }
  156.     /// <remarks/>
  157.     public string Username
  158.     {
  159.         get
  160.         {
  161.             return this.usernameField;
  162.         }
  163.         set
  164.         {
  165.             this.usernameField = value;
  166.         }
  167.     }
  168.     /// <remarks/>
  169.     public ushort Password
  170.     {
  171.         get
  172.         {
  173.             return this.passwordField;
  174.         }
  175.         set
  176.         {
  177.             this.passwordField = value;
  178.         }
  179.     }
  180. }
  181. /// <remarks/>
  182. [System.SerializableAttribute()]
  183. [System.ComponentModel.DesignerCategoryAttribute("code")]
  184. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  185. public partial class ConfigurationServiceConfig
  186. {
  187.     private byte pushTimeSpanField;
  188.     private bool isPushScheduledField;
  189.     private bool isPushChangedField;
  190.     /// <remarks/>
  191.     public byte PushTimeSpan
  192.     {
  193.         get
  194.         {
  195.             return this.pushTimeSpanField;
  196.         }
  197.         set
  198.         {
  199.             this.pushTimeSpanField = value;
  200.         }
  201.     }
  202.     /// <remarks/>
  203.     public bool IsPushScheduled
  204.     {
  205.         get
  206.         {
  207.             return this.isPushScheduledField;
  208.         }
  209.         set
  210.         {
  211.             this.isPushScheduledField = value;
  212.         }
  213.     }
  214.     /// <remarks/>
  215.     public bool IsPushChanged
  216.     {
  217.         get
  218.         {
  219.             return this.isPushChangedField;
  220.         }
  221.         set
  222.         {
  223.             this.isPushChangedField = value;
  224.         }
  225.     }
  226. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表