c#自定义Json类

类名:

public class Messageback
{
public string errcode { get; set; }
public string errmsg { get; set; }
}

public class MessageInfo
{
public string touser { get; set; }
//public string toparty { get; set; }
public string msgtype { get; set; }
public string agentid { get; set; }
public List<Txt_content> text { get; set; }
}
public class NewsInfo
{
public string touser { get; set; }
public string msgtype { get; set; }
public string agentid { get; set; }
public string news { get; set; }
}

public class news_articles
{
public List<articles_info> articles { get; set; }
}

public class articles_info
{
public string title { get; set; }
public string description { get; set; }
public string url { get; set; }
public string picurl { get; set; }
}

public class Txt_content
{
public string content{get;set;}
}

 

方法:

protected void QunFa_Click(object sender, EventArgs e)
{
MessageInfo msinfo = new MessageInfo();
int Row = this.CheckBoxList1.Items.Count;
int snum = 0;
int fnum = 0;
string OpenID, Agentname, PostData1, PostData2, NewPostData,ToUser;
ArrayList suclist = new ArrayList();
ArrayList faillist = new ArrayList();
for(int i=0;i<Row ;i++)
{
if(this.CheckBoxList1.Items[i].Selected == true)
{
msinfo.touser = this.CheckBoxList1.Items[i].Value;
Agentname = this.CheckBoxList1.Items[i].Text;
msinfo.agentid = "28";
Txt_content txt = new Txt_content { content = Content.Text };
List<Txt_content> list = new List<Txt_content>();
list.Add(txt);
msinfo.text = list;
msinfo.msgtype = "text";
string res = "";
string Access_Token = IsExistAccess_Token();

string posturl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Access_Token;
string postData = JsonConvert.SerializeObject(msinfo);
PostData1 = postData.Replace("[", "");
PostData2 = PostData1.Replace("]", "");
res = GetPage(posturl, PostData2);
//微信消息返回处理
JObject msgback = JObject.Parse(res);
//Response.Write(res);
int suc = Convert.ToInt32(msgback["errcode"].ToString());
if(suc==0)
{
suclist.Add(Agentname);
snum = snum + 1;
}
else
{
faillist.Add(Agentname);
fnum = fnum + 1;
}
}
}
txtalert.InnerHtml = "<b>共" + snum + "名员工短信发送成功! "+ fnum +"名发送失败!</b><br/>";
//输出人员名单
txtalert.InnerHtml += "<b>发送成功人员名单:</b><br/>";
int snum_temp = 0;
foreach (string suclist_temp in suclist)
{
//标点符号处理
snum_temp = snum_temp + 1;
if(snum_temp==suclist.Count)
{
txtalert.InnerHtml += suclist_temp;
}
else
{
txtalert.InnerHtml += suclist_temp;
txtalert.InnerHtml += ",";
}

}
txtalert.InnerHtml += "<br/><b>发送失败人员名单:</b><br/>";
int fnum_temp = 0;
foreach (string faillist_temp in faillist)
{
//标点符号处理
fnum_temp = fnum_temp + 1;
if(fnum_temp==faillist.Count)
{
txtalert.InnerHtml += faillist_temp;
}
else
{
txtalert.InnerHtml += faillist_temp;
txtalert.InnerHtml += ",";
}

}
//alert.InnerHtml += "<br/>成功人员名单:"+ +"。<br/>失败人员名单:"+ faillist.ToString() +"";
//Response.Write("共有" + num + "名员工短信发送成功!");
}

protected void tuwen_Click(object sender, EventArgs e)
{
NewsInfo newsinfo = new NewsInfo();
news_articles news_art = new news_articles();
articles_info art_info = new articles_info();
ArrayList suclist = new ArrayList();
ArrayList faillist = new ArrayList();
int Row = this.CheckBoxList1.Items.Count;
int snum = 0;
int fnum = 0;
string tt = this.title.Text;
string des = this.descrip.Text;
string url = this.url.Text;//图文链接
string picurl = this.picurl.Text;//封面图片
int num = 0;
string OpenID, PostData1, PostData2, Agentname;
for (int i = 0; i < Row; i++)
{
if (this.CheckBoxList1.Items[i].Selected == true)
{
Agentname = this.CheckBoxList1.Items[i].Text;
newsinfo.touser = this.CheckBoxList1.Items[i].Value;
newsinfo.msgtype = "news";
newsinfo.agentid = "28";
//art_info.title = Agentname + "同学,今天是你的生日,祝你生日快乐~!";
art_info.title = tt;
art_info.description = des;
art_info.url = url;
art_info.picurl = picurl;
List<articles_info> list = new List<articles_info>();
list.Add(art_info);
news_art.articles = list;
newsinfo.news = JsonConvert.SerializeObject(news_art).Replace("\\","");
string res = "";
string Access_Token = IsExistAccess_Token();
string postData = JsonConvert.SerializeObject(newsinfo);
PostData1 = postData.Replace("\"[", "[");
PostData2 = PostData1.Replace("]\"", "]");
PostData2 = PostData2.Replace("\":\"{", "\":{");
PostData2 = PostData2.Replace("}\"}", "}}");
PostData2 = PostData2.Replace("\\", "");
string posturl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Access_Token;
//string postData = "{\"touser\":\"" + OpenID +"\",\"msgtype\":\"news\",\"news\":{\"articles\":[{\"title\":\""+ tt +"\",\"description\":\""+ des +"\",\"url\":\""+ url +"\",\"picurl\":\""+ picurl +"\"}]}";
res = GetPage(posturl,PostData2);

JObject msgback = JObject.Parse(res);
//Response.Write(res);
int suc = Convert.ToInt32(msgback["errcode"].ToString());
if (suc == 0)
{
suclist.Add(Agentname);
snum = snum + 1;
}
else
{
faillist.Add(Agentname);
fnum = fnum + 1;
}
}

}
newsalert.InnerHtml = "<b>共" + snum + "名员工图文消息发送成功! " + fnum + "名发送失败!</b><br/>";
//输出人员名单
newsalert.InnerHtml += "<b>发送成功人员名单:</b><br/>";
int snum_temp = 0;
foreach (string suclist_temp in suclist)
{
//标点符号处理
snum_temp = snum_temp + 1;
if (snum_temp == suclist.Count)
{
newsalert.InnerHtml += suclist_temp;
}
else
{
newsalert.InnerHtml += suclist_temp;
newsalert.InnerHtml += ",";
}

}
newsalert.InnerHtml += "<br/><b>发送失败人员名单:</b><br/>";
int fnum_temp = 0;
foreach (string faillist_temp in faillist)
{
//标点符号处理
fnum_temp = fnum_temp + 1;
if (fnum_temp == faillist.Count)
{
newsalert.InnerHtml += faillist_temp;
}
else
{
newsalert.InnerHtml += faillist_temp;
newsalert.InnerHtml += ",";
}

}
}

posted @ 2015-11-06 16:42  皮皮木有小JJ  阅读(2433)  评论(0编辑  收藏  举报