Skip to content

Commit

Permalink
反序列化JSON到给定的匿名对象
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmey-Jiang committed Jan 5, 2018
1 parent bfe1fb0 commit 1c7e92e
Show file tree
Hide file tree
Showing 29 changed files with 90,862 additions and 1 deletion.
Binary file modified Utility基础类大全/.vs/Common.Utility/v15/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Utility基础类大全/Common.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>bin\Debug\Microsoft.Office.Interop.Owc11.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>packages\Newtonsoft.Json.11.0.1-beta3\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NPOI, Version=1.2.5.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Common.Utility\Utility基础类大全\bin\Debug\NPOI.dll</HintPath>
Expand Down Expand Up @@ -597,6 +602,7 @@
<Content Include="计划任务\计划任务使用说明.txt" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages\Common.Logging.3.3.1\Common.Logging.3.3.1.nupkg" />
<None Include="packages\Common.Logging.Core.3.3.1\Common.Logging.Core.3.3.1.nupkg" />
<None Include="packages\EntityFramework.5.0.0\Content\App.config.transform" />
Expand Down
97 changes: 97 additions & 0 deletions Utility基础类大全/JSON操作/ConvertJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Reflection;
using System.Collections;
using System.Data.Common;
using Newtonsoft.Json;
using System.IO;

namespace Common.Utility
{
Expand Down Expand Up @@ -319,5 +321,100 @@ public static string ToJson(DbDataReader dataReader)
return jsonString.ToString();
}
#endregion


#region Datatable转换为Json 2

/// <summary>
/// Datatable转换为Json 2
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<Dictionary<string, object>> DataTableToDictionary(DataTable dt)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
return list;
}
#endregion


#region SerializeObject

/// <summary>
/// SerializeObject
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string SerializeObject(object o)
{
string json = JsonConvert.SerializeObject(o);
return json;
}
#endregion


#region 解析JSON字符串生成对象实体

/// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
/// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
}

#endregion

#region 解析JSON数组生成对象实体集合

/// <summary>
/// 解析JSON数组生成对象实体集合
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
/// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
}

#endregion

#region 反序列化JSON到给定的匿名对象

/// <summary>
/// 反序列化JSON到给定的匿名对象.
/// </summary>
/// <typeparam name="T">匿名对象类型</typeparam>
/// <param name="json">json字符串</param>
/// <param name="anonymousTypeObject">匿名对象</param>
/// <returns>匿名对象</returns>
public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject)
{
T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
return t;
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4601c699269147baa49c7900835656b5a1c40a49
1ecb633395df6fd06dce2564169cfe8e52f32209
Binary file not shown.
4 changes: 4 additions & 0 deletions Utility基础类大全/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.1-beta3" targetFramework="net40" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2007 James Newton-King

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 1c7e92e

Please sign in to comment.