Esempio n. 1
0
 /// <summary>
 ///  split json to dicationary array
 /// <para>将Json 数组分隔成多个键值对。</para>
 /// </summary>
 public static List <Dictionary <string, string> > SplitArray(string jsonArray)
 {
     if (string.IsNullOrEmpty(jsonArray))
     {
         return(null);
     }
     jsonArray = jsonArray.Trim();
     return(JsonSplit.Split(jsonArray));
 }
Esempio n. 2
0
        /// <summary>
        ///  split json to dicationary
        /// <para>将Json分隔成键值对。</para>
        /// </summary>
        public static Dictionary <string, string> Split(string json)
        {
            List <Dictionary <string, string> > result = JsonSplit.Split(json);

            if (result != null && result.Count > 0)
            {
                return(result[0]);
            }
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        ///  split json to dicationary
        /// <para>将Json分隔成键值对。</para>
        /// </summary>
        public static Dictionary <string, string> Split(string json)
        {
            json = json.Trim();
            if (json[0] != '{' && json[0] != '[')
            {
                json = ToJson(json);
            }
            List <Dictionary <string, string> > result = JsonSplit.Split(json);

            if (result != null && result.Count > 0)
            {
                return(result[0]);
            }
            return(null);
        }
Esempio n. 4
0
        /// <summary>
        /// Get json value
        /// <para>获取Json字符串的值</para>
        /// </summary>
        /// <param name="key">the name or key of json
        /// <para>键值(有层级时用:XXX.YYY.ZZZ)</para></param>
        /// <returns></returns>
        public static string GetValue(string json, string key)
        {
            string result = string.Empty;

            if (!string.IsNullOrEmpty(json))
            {
                List <Dictionary <string, string> > jsonList = JsonSplit.Split(json);
                if (jsonList.Count > 0)
                {
                    string[] items = key.Split('.');
                    string   fKey  = items[0];
                    int      i     = -1;
                    int      fi    = key.IndexOf('.');
                    if (int.TryParse(fKey, out i))//数字
                    {
                        if (i < jsonList.Count)
                        {
                            Dictionary <string, string> numJson = jsonList[i];
                            if (items.Length == 1)
                            {
                                result = ToJson(numJson);
                            }
                            else if (items.Length == 2) // 0.xxx
                            {
                                string sKey = items[1];
                                if (numJson.ContainsKey(sKey))
                                {
                                    result = numJson[sKey];
                                }
                            }
                            else
                            {
                                return(GetValue(ToJson(numJson), key.Substring(fi + 1)));
                            }
                        }
                    }
                    else // 非数字
                    {
                        Dictionary <string, string> jsonDic = jsonList[0];
                        if (items.Length == 1)
                        {
                            if (jsonDic.ContainsKey(fKey))
                            {
                                result = jsonDic[fKey];
                            }
                        }
                        else  // 取子集
                        {
                            return(GetValue(jsonDic[fKey], key.Substring(fi + 1)));
                        }
                    }



                    //int fi = key.IndexOf('.');
                    //if (jsonDic.ContainsKey(key))
                    //{
                    //    result = jsonDic[key];
                    //}
                    //else
                    //{

                    //    if (fi > -1)
                    //    {
                    //        string fKey = key.Substring(0, fi);//0.abc
                    //        if (jsonDic.ContainsKey(fKey))//0
                    //        {
                    //            return GetValue(jsonDic[fKey], key.Substring(fi + 1));
                    //        }
                    //        else
                    //        {
                    //            int index = -1;
                    //            if (int.TryParse(fKey, out index))//数字,走索引
                    //            {

                    //            }
                    //        }
                    //    }
                    //}
                    //jsonDic = null;
                    //jsonList = null;
                }
            }
            return(result);
        }