1. 程式人生 > >C#使用NewtonSoft操作Json

C#使用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 |