C#使用json
点击工具 - NuGet包管理器 - 程序包管理控制台,输入下面命令:
Install-Package Newtonsoft.Json
即可使用Newtonsoft.Json 包进行json操作
PM> Install-Package Newtonsoft.Json
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Data;
using Newtonsoft.Json.Converters;
namespace Dd.Utility
{
public static class JsonHelper
{
/// <summary>
/// Json To Object
/// </summary>
/// <param name="json">Json</param>
/// <returns>Object</returns>
public static object ToObjct(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject(json);
}
/// <summary>
/// Object To Json
/// </summary>
/// <param name="obj">Object</param>
/// <returns>Json</returns>
public static string ToJson(this object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// Json To Object T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Json"></param>
/// <returns></returns>
public static T ToObject<T>(this string json)
{
return json == null ? default(T) : JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// Json To List
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Json"></param>
/// <returns></returns>
public static List<T> ToList<T>(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject<List<T>>(json);
}
/// <summary>
/// Json To Table
/// </summary>
/// <param name="Json"></param>
/// <returns></returns>
public static DataTable JsonToTable(this string json)
{
return json == null ? null : JsonConvert.DeserializeObject<DataTable>(json);
}
/// <summary>
/// Table To Json
/// </summary>
/// <param name="dataTable"></param>
/// <returns></returns>
public static string TableToJson(this DataTable dataTable)
{
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
return dataTable == null ? "" : JsonConvert.SerializeObject(dataTable, new DataTableConverter(), timeFormat);
}
}
}
var user = new { id = "", name = "", sex = "", age = "" };
user = JsonConvert.DeserializeAnonymousType("{\"id\":\"1\",\"name\":\"张三\",\"sex\":\"男\",\"age\":\"18\"}", user);
Console.WriteLine(user.id + " " + user.name + " " + user.sex + " " + user.age);
//输出结果:1 张三 男 18
/////////////////////////////////////////////////////////////////////////////////
var userList = new[] { new { id = "1", name = "张三", sex = "男", age = "18" }, new { id = "2", name = "李四", sex = "女", age = "17" } };
//匿名序列化集合
string userSerialize = JsonConvert.SerializeObject(userList);
Console.WriteLine(userSerialize);
//输出结果:[{"id":"1","name":"张三","sex":"男","age":"18"},{"id":"2","name":"李四","sex":"女","age":"17"}]
//匿名反序列化集合
var userDeserialize = JsonConvert.DeserializeAnonymousType(userSerialize, new[] { new { id = "", name = "", sex = "", age = "" } });
foreach (var userTemp in userDeserialize)
{
Console.Write(userTemp.id + " " + userTemp.name + " " + userTemp.sex + " " + userTemp.age+" | ");
}
//输出结果:1 张三 男 18 | 2 李四 女 17 |
/////////////////////////////////////////////////////////////////////////////////
JArray jArrayUser = JArray.Parse("[{\"id\":\"1\",\"name\":\"张三\",\"sex\":\"男\",\"age\":\"18\"},{\"id\":\"2\",\"name\":\"李四\",\"sex\":\"女\",\"age\":\"17\"}]");
foreach (JObject jUser in jArrayUser)
{
Console.Write(jUser["id"].ToString() + " " + jUser["name"].ToString() + " " + jUser["sex"].ToString() + " " + jUser["age"].ToString() + " | ");
}
//输出结果:1 张三 男 18 | 2 李四 女 17 |