日期:2014-05-17 浏览次数:20405 次
public string ObjectToJson<T>(string jsonName, IList<T> t)
{
StringBuilder Json = new StringBuilder();
Json.Append("{\"" + jsonName + "\":[");
if (t.Count > 0)
{
for (int i = 0; i < t.Count; i++)
{
T obj = Activator.CreateInstance<T>();
Type type = obj.GetType();
PropertyInfo[] pis = type.GetProperties();
Json.Append("{");
for (int j = 0; j < pis.Length; j++)
{
Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(t[i], null) + "\"");
if (j < pis.Length - 1)
{
Json.Append(",");
}
else
{
Json.Append("}");<