Json轉換(一)
///
/// List轉成json
///
///
///
///
///
public static string ListToJson(IList list, string jsonName, int? p_rowCount)
{
StringBuilder Json = new StringBuilder();
if (string.IsNullOrEmpty(jsonName))
{
jsonName = "rows";
}
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("{\"total\":" + count + ",\"" + jsonName + "\":["); //\"totalCount\":" + count + ",
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance();
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}"); //]}
return Json.ToString();
}
///
///List轉成json(不需要JSONname時使用該方法)
///
///
///
///
public static string ListToJson(List list,string jsonName, int? p_rowCount)
{
StringBuilder Json = new StringBuilder();
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("{\"total\":" + count + ",\"" + jsonName + "\":[");
// Json.Append("[");
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance();
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
///
///List轉成json(不需要JSONname時使用該方法)
///
///
///
///
public static string ListToJson(List list,int? p_rowCount)
{
StringBuilder Json = new StringBuilder();
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("[");
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance();
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]");
return Json.ToString();
}
注:轉換(一二三)位於同一個Static類中
/// List轉成json
///
///
///
///
///
public static string ListToJson
{
StringBuilder Json = new StringBuilder();
if (string.IsNullOrEmpty(jsonName))
{
jsonName = "rows";
}
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("{\"total\":" + count + ",\"" + jsonName + "\":["); //\"totalCount\":" + count + ",
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}"); //]}
return Json.ToString();
}
///
///List轉成json(不需要JSONname時使用該方法)
///
///
///
///
public static string ListToJson
{
StringBuilder Json = new StringBuilder();
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("{\"total\":" + count + ",\"" + jsonName + "\":[");
// Json.Append("[");
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
///
///List轉成json(不需要JSONname時使用該方法)
///
///
///
///
public static string ListToJson
{
StringBuilder Json = new StringBuilder();
int? count = 0;
if (p_rowCount == null)
{
count = list.Count;
}
else
{
count = p_rowCount;
}
Json.Append("[");
if (count > 0)
{
for (int i = 0; i < list.Count; i++)
{
T bj = Activator.CreateInstance
PropertyInfo[] pi = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pi.Length; j++)
{
Type type = pi[j].PropertyType; //.GetValue(list[i], null) ?? string.Empty;
object o = pi[j].GetValue(list[i], null) ?? string.Empty;
Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(o.ToString(), type));
if (j < pi.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]");
return Json.ToString();
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-759361/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Json轉換(二)JSON
- Json轉換(三)JSON
- 一個簡單的Json轉換器JSON
- java json字串轉換為物件,轉換為listJavaJSON字串物件
- eval() JSON轉換為物件JSON物件
- 自寫Json轉換工具JSON
- 資料集轉換JSONJSON
- json字串和json格式物件的轉換JSON字串物件
- Map 轉json資料,json資料轉換為MapJSON
- json字串與物件互相轉換JSON字串物件
- C#中JSON轉換類C#JSON
- json字串 轉換為陣列JSON字串陣列
- JSON字串與HashMap相互轉換JSON字串HashMap
- xStream完美轉換XML、JSONXMLJSON
- php陣列轉換為json,json又轉化為php物件。PHP陣列JSON物件
- JavaScript:如何將JSON物件轉換成JSON字串呢JavaScriptJSON物件字串
- Gson轉換 — json資料轉換為Object實體公共方法JSONObject
- 將json資料轉換為Python字典將json資料轉換為Python字典JSONPython
- eval() JSON轉換 小括號 () 作用JSON
- JSON字串轉換為物件直接量JSON字串物件
- js 陣列,字串,json互相轉換陣列字串JSON
- 將json字串轉換成list<T>JSON字串
- php json轉換成陣列形式PHPJSON陣列
- Go語言轉換JSON資料GoJSON
- Java與Json資料格式轉換JavaJSON
- 最有效的ResultSet轉換為JSON?JSON
- js 將json字串轉換為json物件的方法解析JSON字串物件
- Android中實現JSON字串和JSON物件的轉換AndroidJSON字串物件
- JavaScript將物件轉換為JSON格式字串JavaScript物件JSON字串
- eval()將JSON格式字串轉換為物件JSON字串物件
- Json,String,Map之間的轉換JSON
- JavaScript將JSON格式字串轉換為物件JavaScriptJSON字串物件
- 【python】str與json型別轉換PythonJSON型別
- iOS 手動打造JSON Model轉換庫iOSJSON
- js 將json字串轉換為json格式物件程式碼例項JSON字串物件
- Python將xml格式轉換為json格式PythonXMLJSON
- Spring Boot之自定義JSON轉換器Spring BootJSON
- json字串和js物件之間相互轉換JSON字串物件