GetJsonNodeValue , GetJsonNodeValueFromFirstArrayItem 取JSON节点的值


using Newtonsoft.Json


    public static string GetJsonNodeValue(string key, string sContent)
        {
            if (string.IsNullOrEmpty(sContent)) return "";

            //JObject jo = (JObject)JsonConvert.DeserializeObject(sContent);
            //var item = jo[key];
            //if (item != null && item.ToString().Trim() != "")
            //{
            //    return item.ToString();
            //}

            // JArray ja = (JArray)JsonConvert.DeserializeObject(sContent);

            try
            {
                dynamic ja1 = JsonConvert.DeserializeObject(sContent);
                var item1 = ja1[key];
                if (item1 != null && item1.ToString().Trim() != "")
                {
                    return item1.ToString();
                }
            }
            catch (Exception ex)
            {

            }

            return "";
        }

        public static string GetJsonNodeValueFromFirstArrayItem(string key, string sContent)
        {
            if (string.IsNullOrEmpty(sContent)) return "";

            //JObject jo = (JObject)JsonConvert.DeserializeObject(sContent);
            //var item = jo[key];
            //if (item != null && item.ToString().Trim() != "")
            //{
            //    return item.ToString();
            //}

            // JArray ja = (JArray)JsonConvert.DeserializeObject(sContent);

            try
            {
                dynamic ja1 = JsonConvert.DeserializeObject(sContent);
                var item1 = ja1[0][key];
                if (item1 != null && item1.ToString().Trim() != "")
                {
                    return item1.ToString();
                }
            }
            catch (Exception ex)
            {

            }

            return "";
        }


你可能感兴趣的:(GetJsonNodeValue , GetJsonNodeValueFromFirstArrayItem 取JSON节点的值)