/// <summary> /// GetQuery /// </summary> /// <param name="strName"></param> /// <returns></returns> public static string[] GetQuerys(string strName) { string ids = GetString(strName); List <string> result = new List <string>(); var array = ids.Split(','); foreach (var a in array) { if (JosonValidate.NotIsNullOrEmpty(a)) { result.Add(a); } else { result.Add("Null"); } } return(result.ToArray()); }
/// <summary> /// List转成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonName"></param> /// <param name="list"></param> /// <returns></returns> public static string ToJson <T>(this IList <T> list, string jsonName) { StringBuilder Json = new StringBuilder(); if (string.IsNullOrEmpty(jsonName)) { if (list.Count > 1) { jsonName = list[0].GetType().Name; } else { jsonName = list.GetType().FullName; } } Json.Append("{\"" + jsonName + "\":["); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { T obj = Activator.CreateInstance <T>(); PropertyInfo[] pi = obj.GetType().GetProperties(); Json.Append("{"); for (int j = 0; j < pi.Length; j++) { Type type = pi[j].PropertyType; String strValue = string.Empty; #region 查看其是否Attributes bool IsIgnoreAttribute = false; #region 取出所以属性时 需要遍历 /* * // 取出所以属性 * object[] objAttribute = pi[j].GetCustomAttributes(typeof(Attribute), true); * * for (int attrU = 0; attrU < objAttribute.Length; attrU++) * { * ScriptIgnoreAttribute JosonAttribute = objAttribute[attrU] as ScriptIgnoreAttribute; * * if (JosonAttribute != null) * { * IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); * } * * } */ #endregion object[] objAttributes = pi[j].GetCustomAttributes(typeof(ScriptIgnoreAttribute), true); if (objAttributes.Length > 0) { ScriptIgnoreAttribute JosonAttribute = objAttributes[0] as ScriptIgnoreAttribute; if (JosonAttribute != null) { IsIgnoreAttribute = JosonValidate.NotIsNullOrEmpty(JosonAttribute.GetType().Name.ToStrings()); } } #endregion // 忽略有IgnoreAttribute 的属性 if (!IsIgnoreAttribute) { #region 反射获取属性及值 if (type.IsGenericType) { //是否泛型 #region 泛型Nullable<> Type genericTypeDefinition = pi[j].PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable <>)) { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(List[0], string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); object o = pi[j].GetValue(list[i], null); type = Nullable.GetUnderlyingType(type); strValue = Convert.ToString(o); } #endregion } else { //String value = Convert.ToString(pi[j].GetValue(list[i], null)); //pi[j].SetValue(Lists, string.IsNullOrEmpty(value) ? null : Convert.ChangeType(value, Nullable.GetUnderlyingType(type)), null); type = pi[j].GetValue(list[i], null).ToStrings().GetType(); strValue = pi[j].GetValue(list[i], null).ToStrings(); } #endregion Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(strValue, type)); if (j < pi.Length - 1) { Json.Append(","); } } } Json.Append("}"); if (i < list.Count - 1) { Json.Append(","); } } } Json.Append("]}"); return(Json.ToString()); }