Json轉換(二)
///
/// 物件轉換為Json字串
///
/// 物件
///Json字串
public static string ToJson(object jsonObject)
{
string jsonString = "{";
PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
string value = string.Empty;
object bjectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
//當VO中的屬性是其他VO即一對多並且是泛型
if (propertyInfo[i].GetValue(jsonObject, null)!=null&&propertyInfo[i].GetValue(jsonObject, null).GetType().IsGenericType)
{
object bj = propertyInfo[i].GetValue(jsonObject, null);
Type bjType = obj.GetType();
int count = Convert.ToInt32(objType.GetProperty("Count").GetValue(obj, null));
if (count > 0)
{
for (int k = 0; k < count; k++)
{
object listItem = objType.GetProperty("Item").GetValue(obj, new object[] { k });
if(k value += ToJson(listItem)+",";
else
value += ToJson(listItem);
}
jsonString += "\"" + propertyInfo[i].Name + "\":[" + value + "],";
}
}
else
{
//當VO中的屬性是其他VO
Type type = propertyInfo[i].PropertyType;
if (type.FullName.Contains("PART.Models"))
{
PropertyInfo[] subpropertyInfo = objectValue.GetType().GetProperties();
if (subpropertyInfo.Length > 0)
{
value = ToJson(objectValue);
jsonString += "\"" + propertyInfo[i].Name + "\":" + value + ",";
}
}
else
{
value = bjectValue == null ? "" : objectValue.ToString();
jsonString += "\"" + propertyInfo[i].Name + "\":" + StringFormat(value, type) + ",";
}
}
}
jsonString=jsonString.Remove(jsonString.Length - 1,1);
return jsonString + "}";
}
///
/// 格式化字元型、日期型、布林型
///
///
///
///
private static string StringFormat(string str, Type type)
{
Type p_date=Type.GetType("string");
if (type == typeof(string))
{
str = String2Json(str);
str = "\"" + str + "\"";
}
else if (type == typeof(DateTime) ||IsNullableDate(type))
{
if (str!="")
str = "\"" + (Convert.ToDateTime(str)).ToString("yyyy/MM/dd") + "\"";
else
str = "\"" + str + "\"";
}
else if (type == typeof(bool))
{
str = str.ToLower();
}
else if (type == typeof(IEnumerable))
{
str = "\"" + ((IEnumerable)str).ToString() + "\"";
}
else
{
str = "\"" + str + "\"";
}
return str;
}
private static bool IsNullableDate(Type type)
{
bool flag = false;
if (type.ToString().Contains("System.Nullable"))
{
string m_type = type.ToString().Substring(type.ToString().IndexOf('[') + 1);
m_type = m_type.Remove(m_type.LastIndexOf(']'));
if(m_type.Equals("System.DateTime"))
flag = true;
}
return flag;
}
///
/// 過濾特殊字元
///
///
///
public static string String2Json(String s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s.ToCharArray()[i];
switch (c)
{
case '\"':
sb.Append("\\\""); break;
case '\\':
sb.Append("\\\\"); break;
case '/':
sb.Append("\\/"); break;
case '\b':
sb.Append("\\b"); break;
case '\f':
sb.Append("\\f"); break;
case '\n':
sb.Append("\\n"); break;
case '\r':
sb.Append("\\r"); break;
case '\t':
sb.Append("\\t"); break;
default:
sb.Append(c); break;
}
}
return sb.ToString();
}
注:Json轉換(一二三)放在同一個Static類中
/// 物件轉換為Json字串
///
/// 物件
///
public static string ToJson(object jsonObject)
{
string jsonString = "{";
PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
string value = string.Empty;
object bjectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
//當VO中的屬性是其他VO即一對多並且是泛型
if (propertyInfo[i].GetValue(jsonObject, null)!=null&&propertyInfo[i].GetValue(jsonObject, null).GetType().IsGenericType)
{
object bj = propertyInfo[i].GetValue(jsonObject, null);
Type bjType = obj.GetType();
int count = Convert.ToInt32(objType.GetProperty("Count").GetValue(obj, null));
if (count > 0)
{
for (int k = 0; k < count; k++)
{
object listItem = objType.GetProperty("Item").GetValue(obj, new object[] { k });
if(k
else
value += ToJson(listItem);
}
jsonString += "\"" + propertyInfo[i].Name + "\":[" + value + "],";
}
}
else
{
//當VO中的屬性是其他VO
Type type = propertyInfo[i].PropertyType;
if (type.FullName.Contains("PART.Models"))
{
PropertyInfo[] subpropertyInfo = objectValue.GetType().GetProperties();
if (subpropertyInfo.Length > 0)
{
value = ToJson(objectValue);
jsonString += "\"" + propertyInfo[i].Name + "\":" + value + ",";
}
}
else
{
value = bjectValue == null ? "" : objectValue.ToString();
jsonString += "\"" + propertyInfo[i].Name + "\":" + StringFormat(value, type) + ",";
}
}
}
jsonString=jsonString.Remove(jsonString.Length - 1,1);
return jsonString + "}";
}
///
/// 格式化字元型、日期型、布林型
///
///
///
///
private static string StringFormat(string str, Type type)
{
Type p_date=Type.GetType("string");
if (type == typeof(string))
{
str = String2Json(str);
str = "\"" + str + "\"";
}
else if (type == typeof(DateTime) ||IsNullableDate(type))
{
if (str!="")
str = "\"" + (Convert.ToDateTime(str)).ToString("yyyy/MM/dd") + "\"";
else
str = "\"" + str + "\"";
}
else if (type == typeof(bool))
{
str = str.ToLower();
}
else if (type == typeof(IEnumerable))
{
str = "\"" + ((IEnumerable)str).ToString() + "\"";
}
else
{
str = "\"" + str + "\"";
}
return str;
}
private static bool IsNullableDate(Type type)
{
bool flag = false;
if (type.ToString().Contains("System.Nullable"))
{
string m_type = type.ToString().Substring(type.ToString().IndexOf('[') + 1);
m_type = m_type.Remove(m_type.LastIndexOf(']'));
if(m_type.Equals("System.DateTime"))
flag = true;
}
return flag;
}
///
/// 過濾特殊字元
///
///
///
public static string String2Json(String s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s.ToCharArray()[i];
switch (c)
{
case '\"':
sb.Append("\\\""); break;
case '\\':
sb.Append("\\\\"); break;
case '/':
sb.Append("\\/"); break;
case '\b':
sb.Append("\\b"); break;
case '\f':
sb.Append("\\f"); break;
case '\n':
sb.Append("\\n"); break;
case '\r':
sb.Append("\\r"); break;
case '\t':
sb.Append("\\t"); break;
default:
sb.Append(c); break;
}
}
return sb.ToString();
}
注:Json轉換(一二三)放在同一個Static類中
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28699126/viewspace-759362/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- eval() JSON轉換為物件JSON物件
- 自寫Json轉換工具JSON
- 資料集轉換JSONJSON
- 將json資料轉換為Python字典將json資料轉換為Python字典JSONPython
- json字串 轉換為陣列JSON字串陣列
- json字串與物件互相轉換JSON字串物件
- C#中JSON轉換類C#JSON
- JSON字串與HashMap相互轉換JSON字串HashMap
- eval() JSON轉換 小括號 () 作用JSON
- 【python】str與json型別轉換PythonJSON型別
- JSON字串轉換為物件直接量JSON字串物件
- Json,String,Map之間的轉換JSON
- js 陣列,字串,json互相轉換陣列字串JSON
- 最有效的ResultSet轉換為JSON?JSON
- Python將xml格式轉換為json格式PythonXMLJSON
- JavaScript將物件轉換為JSON格式字串JavaScript物件JSON字串
- eval()將JSON格式字串轉換為物件JSON字串物件
- Spring Boot之自定義JSON轉換器Spring BootJSON
- jenkins:實現Jenkinsfile與Json的轉換JenkinsJSON
- 資料匯入與預處理實驗二---json格式檔案轉換JSON
- fastjson轉換json時,碰到的那些首字母大小寫轉換的坑!ASTJSON
- Python字典格式與JSON格式的相互轉換PythonJSON
- Python JSON 使用指南:解析和轉換資料PythonJSON
- 為啥我的字典資料轉換不成 json?JSON
- typeof-jsonc 將 JSONC/JSON 轉換為 TypeScript 宣告JSONTypeScript
- JS字串轉換為JSON的四種方法筆記字串JSON筆記
- Java中將Excel轉換為JSON的兩種方法JavaExcelJSON
- 把JSON資料格式轉換為Python的類物件JSONPython物件
- 字串json陣列怎麼轉換成jsonobject型別字串JSON陣列Object型別
- Eclipse安裝GSON,使用GSON轉換Java Object到JSONEclipseJavaObjectJSON
- 二叉樹和森林轉換二叉樹
- eval()進行json轉換時新增小括號()的作用JSON
- Antd:Tree樹形控制元件資料解析(JSON轉換)控制元件JSON
- JS實現JSON物件與URL引數的相互轉換JSON物件
- 如何使用python把json檔案轉換為csv檔案PythonJSON
- json字串轉義格式化後再轉換處理demo StringEscapeUtils.unescapeJavaJSON字串Java
- JSON轉ExcelJSONExcel
- json轉化JSON
- Json物件與Json字串互轉JSON物件字串