前言:
這段時間一直在做一個第三方平台的對接,對接第三方其實無非就是請求調用第三方的相關接口接收返回過來的相關參數。因此在這個過程中就會涉及大量的JSON響應參數或者請求參數轉化為對應的實體類的情況,因為只有轉化為對應的實體類我們才好進行相關的數據操作。那么問題來了,這樣我們在遇到后很多JSON對象的情況下是不是要自己一個一個的去寫對應類的屬性那假如有二三十個那豈不是要瘋了去,其實咱們強大的Visual Studio有一個強大的功能能夠將JSON串自動轉化為對應的類(真的是一個提高工作效率的好方法)。
一、首先進行Json格式化校驗
http://www.bejson.com/ (推薦這個在線工具非常好用)

{ "metaData": { "defaultLang": "zh-CN", "name": "追逐時光者每日一秀", "categoryIds": ["214342106997653504", "214343889333583872"], "tagIds": ["215586040843403264", "212828639341903872"], "residentAGApp": "101144753", "sourceName": "追逐時光者出版社", "sellingMode": 2, "remarks": "你是最棒的", "availableFrom": "2019-01-01T08:00:00Z", "availableBefore": "2020-01-01T10:00:00Z", "autoStatusChange": [{ "status": 0, "changeTime": "string" }], "eduappUsed": true, "eduappPurchased": true, "devProductId": "1001", "distNotifyUrl": "https://www.cnblogs.com/Can-daydayup/", "validityUnit": 5, "validityNum": 1, "includeLessons": true, "typeId": 1001, "teachers": ["212828639341903872"], "mediaType": 3, "needDelivery": true }, "countryCodes": ["CN", "SG"] }
二、復制JSON串,前往Visual Studio找到編輯=》選擇性粘貼=》將JSON粘貼為類:
注意:首先根據自己的需求創建一個對應實體空白類

三、JSON成功轉化的實體類:
namespace Domain.Model { public class Rootobject { public Metadata metaData { get; set; } public string[] countryCodes { get; set; } } public class Metadata { public string defaultLang { get; set; } public string name { get; set; } public string[] categoryIds { get; set; } public string[] tagIds { get; set; } public string residentAGApp { get; set; } public string sourceName { get; set; } public int sellingMode { get; set; } public string remarks { get; set; } public DateTime availableFrom { get; set; } public DateTime availableBefore { get; set; } public Autostatuschange[] autoStatusChange { get; set; } public bool eduappUsed { get; set; } public bool eduappPurchased { get; set; } public string devProductId { get; set; } public string distNotifyUrl { get; set; } public int validityUnit { get; set; } public int validityNum { get; set; } public bool includeLessons { get; set; } public int typeId { get; set; } public string[] teachers { get; set; } public int mediaType { get; set; } public bool needDelivery { get; set; } } public class Autostatuschange { public int status { get; set; } public string changeTime { get; set; } } }
