net/json正则表达式应用 微信定位后根据腾讯地图获取定位地址

来源:互联网 发布:上海数据有限公司 编辑:程序博客网 时间:2024/06/04 18:39

#region 方法 GetData(string url) 获取远程网址信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetData(string url)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            string content = reader.ReadToEnd();
            reader.Close();
            return content;
        }
        #endregion


#region 方法 GetQQMaoAddress(string strlatitude, string strlongitude) 通过经纬度获取QQmap地址

    /// <summary>
    /// 
    /// </summary>
    /// <param name="strlatitude"></param>
    /// <param name="strlongitude"></param>
    /// <returns></returns>
    public string GetQQMaoAddress(string strlatitude, string strlongitude)
    {
        string strResult = "", strOpenId = "";
        try
        {
            if (strlatitude != "" && strlongitude != "")
            {
               
                string strJson = GetData("http://apis.map.qq.com/ws/geocoder/v1/?location=" + strlatitude + "," + strlongitude + "&key=yourkey");
                string strCode = @"\""address\"":\s*""(?'address'[^""]*)""";
                Regex regex = new Regex(strCode, RegexOptions.IgnoreCase);
                if (regex.IsMatch(strJson))
                {
                    MatchCollection matches = regex.Matches(strJson);
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (Match match in matches)
                    {
                        strOpenId = match.Groups["address"].Value;
                    }
                }
                strResult = strOpenId;
            }
        }
        catch
        {
            strResult = string.Empty;
        }
        return strResult;
    }
    #endregion
原创粉丝点击