CRM调用外部接口获取数据
调用接口方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| using (var client = new WebClient()) { string url = "http://test.qizhong.online"; client.Encoding = Encoding.UTF8;
string serviceAddress = url + "/ec/sysConnect/doGetDataByCouponNo.do?couponNo=" + code;
RootObject data = JsonConvert.DeserializeObject<RootObject>(client.DownloadString(serviceAddress)); if ("购车".Equals(data.data.data.couponTypeName)) { string serviceAddress1 = url + "/ec/sysConnect/doUpdateCouponStautsById.do?couponNo=" + code + "&&status=1&&vin=" + vinCodeName; client.DownloadString(serviceAddress1); } else if ("备件".Equals(data.data.data.couponTypeName)) { string serviceAddress1 = url + "/ec/sysConnect/doUpdateCouponStautsById.do?couponNo=" + code + "&&status=0&&vin=" + vinCodeName; client.DownloadString(serviceAddress1); } }
|
解析返回的json数据
先使用postman测试返回的数据格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| { "data": { "data": { "actAddress": "南京", "effectDate": "2021-05-17 16:28:35", "exchangeScore": 0, "exchangeTime": "2021-05-08 13:40:05", "isRead": 1, "couponNo": "FWJF2021050800003", "availMoney": "10000", "uneffectDate": "2021-05-14 14:46:12", "couponId": 6, "content": "", "couponTypeName": "备件", "cashMoney": "10000", "applyDept": "服务备件中心", "mbrId": 4519, "logId": 590, "vin": "LXGCPA336LA000221", "overdueTime": "2021-11-04 13:40:05", "status": 0, "couponTitle": "服务纠纷申请" }, "flag": 1 }, "success": true }
|
把要处理的json字符串复制到 http://json2csharp.chahuo.com/ 得到C#类,如下
把下面类放到代码中,其中第73行和81行,由于类名不能重复,而返回的json却有两个data
,所以使用Code
替换一个Date
类,但后边的data
不能改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| public class Data { public string actAddress { get; set; } public string effectDate { get; set; } public string exchangeScore { get; set; } public string exchangeTime { get; set; } public string isRead { get; set; } public string couponNo { get; set; } public string availMoney { get; set; } public string uneffectDate { get; set; } public string couponId { get; set; } public string content { get; set; } public string couponTypeName { get; set; } public string cashMoney { get; set; } public string applyDept { get; set; } public string mbrId { get; set; } public string logId { get; set; } public string vin { get; set; } public string overdueTime { get; set; } public string status { get; set; } public string couponTitle { get; set; }
}
public class Code { public Data data { get; set; } public string flag { get; set; } }
public class RootObject { public Code data { get; set; } public string success { get; set; } }
|