C#JSONシーケンス化Newtonsoft.Jsonの簡単な使用とDLLファイルのダウンロード
5632 ワード
using Newtonsoft.Json;
//DataTable json
public string DtToJson(DataTable table)
{
string jsonString = string.Empty;
jsonString = JsonConvert.SerializeObject(table);
return jsonString;
}
//List json
public static string ListToJson(List list)
{
return JsonConvert.SerializeObject(list);
}
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
namespace Common.Conversion
{
///
/// JSON
///
public static class JsonSerialize
{
#region JSON
///
/// JSON
///
///
///
///
public static string ObjectToJSON(this T _Object) where T : class
{
return JsonConvert.SerializeObject(_Object);
}
///
/// JSON
///
///
///
public static string ObjectToJSON(this Object _Object)
{
return JsonConvert.SerializeObject(_Object);
}
///
/// JSON
///
///
///
///
public static string ObjectToJSONOfficial(this T _Object) where T : class
{
using (var TempMemoryStream = new MemoryStream())
{
var MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
var MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
return MySerializationString;
}
}
///
/// DataTable JSON
///
/// DataTable
///
public static string DtToJson(DataTable table)
{
string jsonString = string.Empty;
jsonString = JsonConvert.SerializeObject(table);
return jsonString;
}
///
/// List JSON
///
///
///
public static string ListToJson(IList list)
{
DataTable table = ListToDataTable(list);
return DtToJson(table);
}
///
/// list dataTable
///
/// List
///
private static DataTable ListToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
result.Columns.Add(pi.Name, colType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
///
/// List JSON
///
///
///
///
public static string ListToJson(List list)
{
return JsonConvert.SerializeObject(list);
}
#endregion
#region JSON
///
/// JSON
///
///
///
///
public static T JSONToObjectJSON(this string _JSONString) where T : class
{
return JsonConvert.DeserializeObject(_JSONString);
}
///
/// JSON
///
///
///
///
public static T JSONToObjectOfficial(this string _JSONString) where T : class
{
using (var TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JSONString)))
{
var MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
}
}
#endregion
}
}
ダウンロード先:
https://download.csdn.net/download/djk8888/10695449(共通バージョン、3.5、4.0、4.5)
https://download.csdn.net/download/djk8888/11686139(32ビット、64ビット、1.0~4.5.1最完全集合)