摘要:本文将带你了解微信应用开发之JSON解析,自定义菜单,希望本文对大家学微信有所帮助。
本文将带你了解微信应用开发之JSON解析,自定义菜单,希望本文对大家学微信有所帮助。
开发者与微信服务器的通信过程:
一.微信主动发送给开发者信息
二.开发者主动发送给微信信息
上一节简单介绍了第一部分,现在来讨论第二部分。
这里主要涉及到JSON数据的解析和发送,以"自定义菜单创建接口"为例
我们观察这个api:
POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
首先要获得ACCESS_TOKEN,这就要用到”获取access_token"接口了。
GET:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
直接上代码
public string get_acctoken() { string gettokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret"; //generate http request HttpWebRequest req = (HttpWebRequest)WebRequest.Create(gettokenurl); //use GET method to get url's html req.Method = "GET"; //use request to get response HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); //otherwise will return messy code // Encoding htmlEncoding = Encoding.GetEncoding(htmlCharset); StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8); //read out the returned html string respHtml = sr.ReadToEnd(); //上边为读取json数据,下边就是解析了 //传说中的反序列化 //另外,为了方便我在model里新建了一个Access_Token实体 Access_Token j2 = new JavaScriptSerializer().Deserialize
public class Access_Token { public string access_token { get; set; } public int expires_in { get; set; } } 获取到token之后,我们就可以拿来创建自定义菜单了
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
大致需要给这个接口post这样的数据
再贴代码。。。
public string creat_mymenu() { string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + finalstr.acctoken; string data = "{\"button\":[{\"type\":\"click\",\"name\":\"今日歌曲\",\"key\":\"V1001_TODAY_MUSIC\"},{\"type\":\"click\",\"name\":\"歌手简介\",\"key\":\"V1001_TODAY_SINGER\"},{\"name\":\"菜单\",\"sub_button\":[{\"type\":\"view\",\"name\":\"搜索\",\"url\":\"//www.soso.com/\"},{\"type\":\"view\",\"name\":\"视频\",\"url\":\"//v.qq.com/\"},{\"type\":\"click\",\"name\":\"赞一下我们\",\"key\":\"V1001_GOOD\"}]}]}"; System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url); httpWebRequest.Method = "POST"; byte[] postBytes = Encoding.UTF8.GetBytes(data); //httpWebRequest.ContentType = "text/xml"; httpWebRequest.ContentType = "application/json; charset=utf-8";// httpWebRequest.ContentLength = Encoding.UTF8.GetByteCount(data);//strJson为json字符串 Stream stream = httpWebRequest.GetRequestStream(); stream.Write(postBytes, 0, postBytes.Length); stream.Close();//发送完毕,接受返回值 var response = httpWebRequest.GetResponse(); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); String responseString = streamRead.ReadToEnd(); return responseString; } 如果获取token,是一种反序列化,那么现在做的就是序列化了,,,
当然因为菜单的不确定性,没有一个固定的数据结构来存储,于是我就拼字符串了。。。。
其实感觉上边那一坨JSON最好写在文本里,因为菜单很少变动。。。
如果你按照步骤操作,应该会返回
{"errcode":0,"errmsg":"ok"}
这时候快去看你的公共账号,有木有变化?
没有?不用担心,微信服务有差不多24小时的延迟。只要取消关注后重新关注就能正常显示了。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号