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://www.qizhong.online";
string url = "http://test.qizhong.online";
client.Encoding = Encoding.UTF8;

//拼接请求的URL
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
{
/// <summary>
/// 活动地点
/// </summary>
public string actAddress { get; set; }
/// <summary>
/// 生效时间
/// </summary>
public string effectDate { get; set; }
/// <summary>
/// 兑换消耗积分
/// </summary>
public string exchangeScore { get; set; }
/// <summary>
/// 兑换时间
/// </summary>
public string exchangeTime { get; set; }
/// <summary>
/// 是否已读 1-是;0-否
/// </summary>
public string isRead { get; set; }
/// <summary>
/// 代金券编号
/// </summary>
public string couponNo { get; set; }
/// <summary>
/// 剩余金额
/// </summary>
public string availMoney { get; set; }
/// <summary>
/// 失效时间
/// </summary>
public string uneffectDate { get; set; }
public string couponId { get; set; }
public string content { get; set; }
/// <summary>
/// 代金券类型
/// </summary>
public string couponTypeName { get; set; }
/// <summary>
/// 代金券金额
/// </summary>
public string cashMoney { get; set; }
/// <summary>
/// 申请部门
/// </summary>
public string applyDept { get; set; }
public string mbrId { get; set; }
/// <summary>
/// 代金券领取主键
/// </summary>
public string logId { get; set; }
/// <summary>
/// 车辆识别码
/// </summary>
public string vin { get; set; }
/// <summary>
/// 过期时间
/// </summary>
public string overdueTime { get; set; }
/// <summary>
/// 代金券状态:0-待使用;1-已使用;-1-已失效 ;2- 未生效
/// </summary>
public string status { get; set; }
/// <summary>
/// 代金券标题
/// </summary>
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; }
}