ASP.NET WEBAPI 获取微信ticket

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;
}

上一篇:js 字符串转json对象


下一篇:Servlet 与 Ajax 交互一直报status=parsererror