C#使用NewtonSoft操作Json

C#使用NewtonSoft操作JsonusingNewtonsoft.Json;usingNewtonsoft.Json.Linq;usingSystem.Collections.Generic;usingSystem.Data;usingNewtonsoft.Json.Converters;namespaceDd.Utility{publicstaticclassJsonHelper…

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 | 

 

今天的文章C#使用NewtonSoft操作Json分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/9544.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注