愚见未来

人的思想时时刻刻都在进步,如果你早上起床,想起昨天所做的事情是那么幼稚和迂腐,那么恭喜你,你又变得成熟一点了!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Json数据格式转换

Posted on 2011-05-04 14:59  愚见未来  阅读(2034)  评论(1编辑  收藏  举报

下面整理任意类型数据转换成json数据的方法超实用函数:

/// <summary>
/// List转成json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonName"></param>
/// <param name="list"></param>
/// <returns></returns>
public static string ListToJson<T>(IList<T> list, string jsonName)
{
 StringBuilder Json = new StringBuilder();
 if (string.IsNullOrEmpty(jsonName))
  jsonName = list[0].GetType().Name;
 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].GetValue(list[i], null).GetType();
    Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type));

    if (j < pi.Length - 1)
    {
     Json.Append(",");
    }
   }
   Json.Append("}");
   if (i < list.Count - 1)
   {
    Json.Append(",");
   }
  }
 }
 Json.Append("]}");
 return Json.ToString();
}

/// <summary>
/// List转成json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static string ListToJson<T>(IList<T> list)
{
 object obj = list[0];
 return ListToJson<T>(list, obj.GetType().Name);
}

/// <summary>
/// 对象转换为Json字符串
/// </summary>
/// <param name="jsonObject">对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(object jsonObject)
{
 string jsonString = "{";
 PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
 for (int i = 0; i < propertyInfo.Length; i++)
 {
  object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
  string value = string.Empty;
  if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
  {
   value = "'" + objectValue.ToString() + "'";
  }
  else if (objectValue is string)
  {
   value = "'" + ToJson(objectValue.ToString()) + "'";
  }
  else if (objectValue is IEnumerable)
  {
   value = ToJson((IEnumerable)objectValue);
  }
  else
  {
   value = ToJson(objectValue.ToString());
  }
  jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ",";
 }
 jsonString.Remove(jsonString.Length - 1, jsonString.Length);
 return jsonString + "}";
}

/// <summary>
/// 对象集合转换Json
/// </summary>
/// <param name="array">集合对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(IEnumerable array)
{
 string jsonString = "[";
 foreach (object item in array)
 {
  jsonString += ToJson(item) + ",";
 }
 jsonString.Remove(jsonString.Length - 1, jsonString.Length);
 return jsonString + "]";
}

/// <summary>
/// 普通集合转换Json
/// </summary>
/// <param name="array">集合对象</param>
/// <returns>Json字符串</returns>
public static string ToArrayString(IEnumerable array)
{
 string jsonString = "[";
 foreach (object item in array)
 {
  jsonString = ToJson(item.ToString()) + ",";
 }
 jsonString.Remove(jsonString.Length - 1, jsonString.Length);
 return jsonString + "]";
}

/// <summary>
/// Datatable转换为Json
/// </summary>
/// <param name="table">Datatable对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(DataTable dt)
{
 StringBuilder jsonString = new StringBuilder();
 jsonString.Append("[");
 DataRowCollection drc = dt.Rows;
 for (int i = 0; i < drc.Count; i++)
 {
  jsonString.Append("{");
  for (int j = 0; j < dt.Columns.Count; j++)
  {
   string strKey = dt.Columns[j].ColumnName;
   string strValue = drc[i][j].ToString();
   Type type = dt.Columns[j].DataType;
   jsonString.Append("\"" + strKey + "\":");
   strValue = StringFormat(strValue, type);
   if (j < dt.Columns.Count - 1)
   {
    jsonString.Append(strValue + ",");
   }
   else
   {
    jsonString.Append(strValue);
   }
  }
  jsonString.Append("},");
 }
 jsonString.Remove(jsonString.Length - 1, 1);
 jsonString.Append("]");
 return jsonString.ToString();
}


文章来自学IT网:http://www.xueit.com/html/2009-12-15/21-1506045328843.html