释蝉

博客园 首页 新随笔 联系 订阅 管理

public static string GetTicket(string AccessToken)
{
//类型
string Type = "jsapi";
string strurl = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type={1}", AccessToken, Type);

var wc = new WebClient();
//取得微信返回的json数据
var strReturn = wc.DownloadString(strurl);
string ticket = GetJsonValue(strReturn, "ticket");
ticket = ticket.Replace("\\", "");
return ticket;

}

 

/// <summary>
/// 获取Json字符串某节点的值
/// </summary>
public static string GetJsonValue(string jsonStr, string key)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(jsonStr))
{
key = "\"" + key.Trim('"') + "\"";
int index = jsonStr.IndexOf(key) + key.Length + 1;
if (index > key.Length + 1)
{
//先截逗号,若是最后一个,截“}”号,取最小值
int end = jsonStr.IndexOf(',', index);
if (end == -1)
{
end = jsonStr.IndexOf('}', index);
}

result = jsonStr.Substring(index, end - index);
result = result.Trim(new char[] { '"', ' ', '\'' }); //过滤引号或空格
}
}
return result;
}

posted on 2021-07-27 13:53  释蝉  阅读(144)  评论(0编辑  收藏  举报