前言:
这段时间一直在做一个第三方平台的对接,对接第三方其实无非就是请求调用第三方的相关接口接收返回过来的相关参数。因此在这个过程中就会涉及大量的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; }
}
}