public void Send()
{
string getToken = "https://api.weixin.qq.com/cgi-bin/token";
string touser = "ou1mFwjZEqFRBdOKaHzYJjRFMsBI";
string tempid = "VrxPh76YptKbDoVWUgVidzlAqNh7xn-iY1neIt8qhGY";
string sendMsg = "https://api.weixin.qq.com/cgi-bin/message/template/send";
string temp = "{\"touser\": \"" + touser + "\"," +
"\"template_id\": \"VrxPh76YptKbDoVWUgVidzlAqNh7xn-iY1neIt8qhGY\", " +
"\"data\": " +
"{\"first\": {\"value\": \"XX家长您好!,温馨提示您,孩子已经开始上课。祝您生活愉快!\"}," +
"\"keyword1\": { \"value\": \"语文课\"}," +
"\"keyword2\": { \"value\": \"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "\"}," +
"\"keyword3\": { \"value\": \"金额\"}," +
"\"remark\": {\"value\": \"剩余10节 系统推送消息,无需回复\" }}}";
JObject obj = new JObject();
obj.Add("touser", touser);
obj.Add("template_id", tempid);
JObject data = new JObject();
JObject first = new JObject();
first.Add("value", "hello");
first.Add("color", "#173177");
JObject keyword1 = new JObject();
keyword1.Add("value", "hello");
keyword1.Add("color", "#173177");
JObject keyword2 = new JObject();
keyword2.Add("value", "hello");
keyword2.Add("color", "#173177");
JObject keyword3 = new JObject();
keyword3.Add("value", "hello");
keyword3.Add("color", "#173177");
JObject remark = new JObject();
remark.Add("value", "hello");
remark.Add("color", "#173177");
data.Add("first", first);
data.Add("keyword1", keyword1);
data.Add("keyword2", keyword2);
data.Add("keyword3", keyword3);
data.Add("remark", remark);
obj.Add("data", data);
var tokenUrl = getToken + "?grant_type=client_credential&appid=appid&secret=secret";
OAuthAccessTokenResult OAuthAccessToken_Model = OAuthApi.GetAccessToken_H5("appid", "e92531dc7cd8c1074b63e72aa4a8e4c1");
//OAuthAccessToken_Model.access_token;
string result = HttpService.Post(obj.ToString().Trim(), sendMsg + "?access_token=" + OAuthAccessToken_Model.access_token, false, 10);
Response.Write(result);
}
或者
Hashtable obj = new Hashtable();
obj.Add("touser", touser);
obj.Add("template_id", tempid);
Hashtable data = new Hashtable();
Hashtable first = new Hashtable();
first.Add("value", "上课开始啦");
first.Add("color", "#173177");
Hashtable keyword1 = new Hashtable();
keyword1.Add("value", "hello");
keyword1.Add("color", "#173177");
Hashtable keyword2 = new Hashtable();
keyword2.Add("value", "hello");
keyword2.Add("color", "#173177");
Hashtable keyword3 = new Hashtable();
keyword3.Add("value", "hello");
keyword3.Add("color", "#173177");
Hashtable remark = new Hashtable();
remark.Add("value", "hello");
remark.Add("color", "#173177");
data.Add("first", first);
data.Add("keyword1", keyword1);
data.Add("keyword2", keyword2);
data.Add("keyword3", keyword3);
data.Add("remark", remark);
obj.Add("data", data);
string data = JoyTools.JsonHelper.ToJson(obj).ToString();
如果用2个.ToJson() 会有空白内容,可以直接用Jobect或拼接字符串
有时候你会发现你的jsonobject中进行转字符串会被转义,然后发出去的模板消息读不到内容,可能是因为你放入的Json字符串,之后又进行了一次jsonObject.toJSONString(),之后就被转义了,建议就是把所有的Json放入到一个jsonObject,最后合并时在进行jsonObject.toJSONString().