c# Json操作
标签:over writer java oid ref out model array lse
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Reflection;
using System.Data;
using Newtonsoft.Json.Linq;
namespace EasyFrame.Common
{
///
/// Json操作
///
public static class Json
{
public static string ToJson(this object obj, string datetimeformats)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
return JsonConvert.SerializeObject(obj, timeConverter);
}
public static List ToList(this string Json)
{
return Json == null ? null : JsonConvert.DeserializeObject>(Json);
}
public static DataTable ToTable(this string Json)
{
return Json == null ? null : JsonConvert.DeserializeObject(Json);
}
public static JObject ToJObject(this string Json)
{
return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", ""));
}
#region 值转换
private static object convertVal(Type t, object data)
{
object val = null;
if (t == typeof(Int32))
val = Convert.ToInt32(data);
else if (t == typeof(DateTime))
val = Convert.ToDateTime(data);
else if (t == typeof(Decimal))
val = Convert.ToDecimal(data);
else if (t == typeof(Boolean))
val = Convert.ToBoolean(data);
else if (t == typeof(String))
val = Convert.ToString(data).Trim();
else if (t == typeof(Guid))
val = new Guid(data.ToString());
else
val = data;
return val;
}
#endregion
#region IToJson 成员
public static string ToJson(object model)
{
if (model == null) return null;
string value = JsonConvert.SerializeObject(model, new AspNetDateTimeConverter());
return value;
}
public static string ToJson(System.Data.DataTable dt)
{
string value = JsonConvert.SerializeObject(dt);
return value;
}
public static string ToJson(string data)
{
object[] objs = new object[] { data };
return ToJson(objs);
}
public static string ToJson(int data)
{
object[] objs = new object[] { data };
return ToJson(objs);
}
public static string ToJson(decimal data)
{
object[] objs = new object[] { data };
return ToJson(objs);
}
public static string ToJson(bool data)
{
object[] objs = new object[] { data };
return ToJson(objs);
}
public static string ToJson(DateTime data)
{
object[] objs = new object[] { data };
return ToJson(objs);
}
public static string ToJson(params object[] data)
{
string value = JsonConvert.SerializeObject(data, new AspNetDateTimeConverter());
return value;
}
#endregion
#region IJsonToObject成员
public static T ToObject(string json)
{
return JsonConvert.DeserializeObject(json);
}
public static object ToObject(string json)
{
return JsonConvert.DeserializeObject(json);
}
public static object[] ToArray(string json)
{
return (ToObject(json) as JArray).ToArray();
}
public static string ToString(string json)
{
return Convert.ToString(ToArray(json)[0]);
}
public static bool ToBoolean(string json)
{
return Convert.ToBoolean(ToArray(json)[0]);
}
public static int ToInt32(string json)
{
return Convert.ToInt32(ToArray(json)[0]);
}
public static decimal ToDecimal(string json)
{
return Convert.ToDecimal(ToArray(json)[0]);
}
public static DateTime ToDateTime(string json)
{
return Convert.ToDateTime(ToArray(json)[0]);
}
public static T ToObject(object data)
{
if (typeof(T).Equals(typeof(int[])))
{
Listint> intvals = new Listint>();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add(Convert.ToInt32((data as JArray)[i]));
}
return (T)(intvals.ToArray() as object);
}
else if (typeof(T).Equals(typeof(string[])))
{
Liststring> intvals = new Liststring>();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add(Convert.ToString((data as JArray)[i]));
}
return (T)(intvals.ToArray() as object);
}
else if (typeof(T).Equals(typeof(decimal[])))
{
Listdecimal> intvals = new Listdecimal>();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add(Convert.ToDecimal((data as JArray)[i]));
}
return (T)(intvals.ToArray() as object);
}
else if (typeof(T).Equals(typeof(Boolean[])))
{
List intvals = new List();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add(Convert.ToBoolean((data as JArray)[i]));
}
return (T)(intvals.ToArray() as object);
}
else if (typeof(T).Equals(typeof(DateTime[])))
{
List intvals = new List();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add(Convert.ToDateTime((data as JArray)[i]));
}
return (T)(intvals.ToArray() as object);
}
else if (typeof(T).Equals(typeof(object[])))
{
Listobject> intvals = new Listobject>();
for (int i = 0; i as JArray).Count; i++)
{
intvals.Add((data as JArray)[i]);
}
return (T)(intvals.ToArray() as object);
}
else
{
T obj = (T)Activator.CreateInstance(typeof(T));
PropertyInfo[] pros = typeof(T).GetProperties();
var oldinttype = typeof(Nullableint>);
var olddectype = typeof(Nullabledecimal>);
var olddoutype = typeof(Nullabledouble>);
var oldbooltype = typeof(Nullable);
var newinttype = typeof(int);
var newdectype = typeof(decimal);
var newdoutype = typeof(double);
var newbooltype = typeof(Boolean);
for (int k = 0; k )
{
object val;
if (pros[k].PropertyType.Equals(oldinttype))
{
if (pros[k].Name == null)
val = default(int);
else
val = convertVal(newinttype, (data as JObject)[pros[k].Name]);
}
else if (pros[k].PropertyType.Equals(olddectype))
{
if (pros[k].Name == null)
val = default(decimal);
else
val = convertVal(newdectype, (data as JObject)[pros[k].Name]);
}
else if (pros[k].PropertyType.Equals(olddoutype))
{
if (pros[k].Name == null)
val = default(double);
else
val = convertVal(newdoutype, (data as JObject)[pros[k].Name]);
}
else if (pros[k].PropertyType.Equals(oldbooltype))
{
if (pros[k].Name == null)
val = default(bool);
else
val = convertVal(newbooltype, (data as JObject)[pros[k].Name]);
}
else
{
val = convertVal(pros[k].PropertyType, (data as JObject)[pros[k].Name]);
}
pros[k].SetValue(obj, val, null);
}
return obj;
}
}
public static object[] ToArray(object data)
{
return (data as JArray).ToArray();
}
public static List ToListObj(object data)
{
if (data is JArray)
{
PropertyInfo[] pros = typeof(T).GetProperties();
List list = new List();
for (int i = 0; i as JArray).Count; i++)
{
T obj = (T)Activator.CreateInstance(typeof(T));
object _data = (data as JArray)[i];
for (int k = 0; k )
{
object val = convertVal(pros[k].PropertyType, (_data as JObject)[pros[k].Name]);
pros[k].SetValue(obj, val, null);
}
list.Add(obj);
}
return list;
}
return null;
}
public static DataTable ToDataTable(object data)
{
if (data is JArray && (data as JArray).Count > 0)
{
JObject _data = (data as JArray)[0] as JObject;
DataTable dt = new DataTable();
foreach (var name in _data)
{
dt.Columns.Add(name.Key, _data[name.Key].GetType());
}
for (int i = 0; i as JArray).Count; i++)
{
object _jsarray = (data as JArray)[i];
DataRow dr = dt.NewRow();
for (int k = 0; k )
{
dr[k] = convertVal(dt.Columns[k].DataType, (_jsarray as JObject)[dt.Columns[k].ColumnName]);
}
dt.Rows.Add(dr);
}
return dt;
}
return null;
}
public static DataTable ToDataTable(string data)
{
return ToDataTable(JsonConvert.DeserializeObject(data));
}
#endregion
}
public class AspNetDateTimeConverter : JavaScriptDateTimeConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(DateTime).IsAssignableFrom(objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DateTime datetime = Convert.ToDateTime(value);
writer.WriteValue(datetime.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
c# Json操作
标签:over writer java oid ref out model array lse
原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9734291.html
评论