這陣子開發工作上常遇到要與第三方做串接的服務,第三方服務提供的技術文件中格式不外乎以 Json 或是 XML 兩種為主,往往都要針對這些屬性自己建立新的類別 Model 的動作,如果 XML 或 JSON 屬性欄位多的話要花不少時間在處理建立對應類別上,但不用擔心,號稱地表上最強的開發 IDE Visual Studio 從 2012 開始提供 Json 與 XML 轉 Class 類別的功能,操作與使用上相當簡單,這篇就來介紹此功能的使用方式。
使用方式
打開 Visual Studio 點選左上角 Edit > Paste Special,就可以看到可以將剪貼簿內容的文字轉換為 Json 或是 XML 的選項,以下就針對兩個選項做簡單的介紹與 Sample
Json 轉 Class
Json 檔案範例是從 .NET Core 專案的 appsetting.json 抓下來,如下所示
Step 1 : 打開 Visual Studio 2017,建立一個新的 Class 類別,接著複製要轉換的 Json 檔案
- {
- "ConnectionStrings": {
- "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ASPCoreLab;Trusted_Connection=True;MultipleActiveResultSets=true"
- },
- "Logging": {
- "LogLevel": {
- "Default": "Warning"
- }
- },
- "AllowedHosts": "*"
- }
Step 2 : Edit > Paste Special > Paste Json to Class
會產生 RootObject 類別物件,其中包含要轉換 Json 類別的屬性 connectionString、logging、Loglevel 其相對應的 Class 類別
- public class Rootobject
- {
- public Connectionstrings ConnectionStrings { get; set; }
- public Logging Logging { get; set; }
- public string AllowedHosts { get; set; }
- }
- public class Connectionstrings
- {
- public string DefaultConnection { get; set; }
- }
- public class Logging
- {
- public Loglevel LogLevel { get; set; }
- }
- public class Loglevel
- {
- public string Default { get; set; }
- }
XML 轉 Class
自己定義一個簡單的 XML 檔案,如下所示
Step 1 : 打開 Visual Studio 2017,建立一個新的 Class 類別,接著複製要轉換的 XML 檔案
- <card>
- <name>Marcus Tung</name>
- <title>打雜工程師</title>
- <email>iamnewuser@gmail.com</email>
- <phone>886 82 5252</phone>
- <logo url="widget.gif"/>
- </card>
Step 2 : Edit > Paste Special > Paste XML to Class
會產生 Rool XML Card 類別物件,並且會自動幫你預設定義好 attribute,像是 Serializable、XML Type 等相關定義,Card 底下的五個屬性有分別定義相關的 get / set,算是非常實用 !
- // NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
- /// <remarks/>
- [System.SerializableAttribute()]
- [System.ComponentModel.DesignerCategoryAttribute("code")]
- [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
- [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
- public partial class card
- {
- private string nameField;
- private string titleField;
- private string emailField;
- private string phoneField;
- private cardLogo logoField;
- /// <remarks/>
- public string name
- {
- get
- {
- return this.nameField;
- }
- set
- {
- this.nameField = value;
- }
- }
- /// <remarks/>
- public string title
- {
- get
- {
- return this.titleField;
- }
- set
- {
- this.titleField = value;
- }
- }
- /// <remarks/>
- public string email
- {
- get
- {
- return this.emailField;
- }
- set
- {
- this.emailField = value;
- }
- }
- /// <remarks/>
- public string phone
- {
- get
- {
- return this.phoneField;
- }
- set
- {
- this.phoneField = value;
- }
- }
- /// <remarks/>
- public cardLogo logo
- {
- get
- {
- return this.logoField;
- }
- set
- {
- this.logoField = value;
- }
- }
- }
- /// <remarks/>
- [System.SerializableAttribute()]
- [System.ComponentModel.DesignerCategoryAttribute("code")]
- [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
- public partial class cardLogo
- {
- private string urlField;
- /// <remarks/>
- [System.Xml.Serialization.XmlAttributeAttribute()]
- public string url
- {
- get
- {
- return this.urlField;
- }
- set
- {
- this.urlField = value;
- }
- }
- }
使用地表上最強 IDE 開發十分方便,連這小細節在早期就開始提供,但知道此功能的人似乎不多有點可惜就是,日後如果有遇到一些不錯的功能在陸續介紹在 Blog 上給需要的朋友,Happy Coding !!
0 意見:
張貼留言