/// <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)); }
/// <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); }
/// <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); }
/// <param name="errIndex">the index of the error char /// <para>错误的字符索引</para></param> public static bool IsJson(string json, out int errIndex) { return(JsonSplit.IsJson(json, out errIndex)); }
/// <summary> /// check string is json /// <para>检测是否Json格式的字符串</para> /// </summary> public static bool IsJson(string json) { return(JsonSplit.IsJson(json)); }
/// <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); }