获取Json数据某节点的值

时间匆忙,直接上代码,回家还得做清蒸鱼呢!

  #region 获取Json字符串某节点的值
        /// <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;
        }
        #endregion

获取XML某节点的值、

#region XML KEY
        /// <summary>
        /// XML KEY  通用方法
        /// </summary>
        /// <returns></returns>
        public static string GetXMLstrByKey(string Key, XmlDocument xml)
        {
            object strValue = xml.SelectSingleNode("xml").SelectSingleNode(Key).InnerText;
            if (strValue != null)
            {
                return strValue.ToString();
            }
            else
            {
                return "";
            }
        }
        #endregion

@陈卧龙的博客

posted @ 2016-11-03 18:42  天才卧龙  阅读(9827)  评论(0编辑  收藏  举报