admin 管理员组文章数量: 1184232
2024年4月13日发(作者:replace函数语法)
c#对象集合转Json
///
/// 普通集合转换Json
///
/// 集合对象
///
public static string ToArrayString(IEnumerable array)
{
string jsonString = "[";
foreach (object item in array)
{
jsonString = ToJson(ng()) + ",";
}
( - 1, );
return jsonString + "]";
}
///
/// 对象集合转换Json
///
/// 集合对象
///
public static string ToJson(IEnumerable array)
{
string jsonString = "[";
foreach (object item in array)
{
jsonString += ToJson(item) + ",";
}
( - 1, );
return jsonString + "]";
}
///
/// 对象转换为Json
///
/// 对象
///
public static string ToJson(object jsonObject)
{
string jsonString = "{";
PropertyInfo[] propertyInfo = e().GetProperties();
for (int i = 0; i < ; i++)
{
object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
string value = ;
if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
{
value = "'" + ng() + "'";
}
else if (objectValue is string)
{
value = "'" + ToJson(ng()) + "'";
}
else if (objectValue is IEnumerable)
{
value = ToJson((IEnumerable)objectValue);
}
else
{
value = ToJson(ng());
}
jsonString += """ + ToJson(propertyInfo[i].Name) + "":" + value + ",";
}
( - 1, );
return jsonString + "}";
}
///
/// List转换成Json
///
public static string ListToJson
{
object obj = list[0];
return ListToJson
}
///
/// List转换成Json
///
public static string ListToJson
{
StringBuilder Json = new StringBuilder();
if (OrEmpty(jsonName)) jsonName = list[0].GetType().Name;
("{"" + jsonName + "":[");
if ( > 0)
{
for (int i = 0; i < ; i++)
{
T obj = Instance
PropertyInfo[] pi = e().GetProperties();
("{");
for (int j = 0; j < ; j++)
{
Type type = pi[j].GetValue(list[i], null).GetType();
(""" + pi[j].ng() + "":" + (pi[j].GetValue(list[i], null).ToString(), type));
if (j < - 1)
{
(",");
}
}
("}");
if (i < - 1)
{
(",");
}
}
}
("]}");
return ng();
}
版权声明:本文标题:c#对象集合转Json 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1713011176a617072.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论