C# Newtonsoft.Json 的使用說明
阿新 • • 發佈:2021-01-14
我就廢話不多說啦,大家還是直接看程式碼吧~
byte[] bUserInfoSearch = new byte[1024 * 10]; //10kb大小 Marshal.Copy(lpBuffer,bUserInfoSearch,bUserInfoSearch.Length); string strUserInfoSearch = System.Text.Encoding.UTF8.GetString(bUserInfoSearch); CUserInfoSearch m_JsonUserInfoSearch = new CUserInfoSearch(); //序列化這個字串 m_JsonUserInfoSearch = JsonConvert.DeserializeObject<CUserInfoSearch>(strUserInfoSearch); public class CUserInfoSearch { public CUserInfoSearchContent UserInfoSearch { get; set; } } public class CUserInfoSearchContent { public string searchID { get; set; } public string responseStatusStrg { get; set; } //查詢狀態字串描述: OK-查詢結束,MORE-還有資料等待查詢,NO MATCH-沒有匹配資料 public int numOfMatches { get; set; } //本次返回的記錄條數 public int totalMatches { get; set; } //符合條件的記錄總條數 public List<CUserInfoContent> UserInfo { get; set; } } public class CUserInfoContent { public string employeeNo { get; set; } public string name { get; set; } public string userType { get; set; } public bool closeDelayEnabled { get; set; } public CVaild Valid { get; set; } public string belongGroup { get; set; } public string password { get; set; } public string doorRight { get; set; } public List<CRightPlan> RightPlan { get; set; } public int maxOpenDoorTime { get; set; } public int openDoorTime { get; set; } public int roomNumber { get; set; } public int floorNumber { get; set; } public bool doubleLockRight { get; set; } public bool alwaysOpenRight { get; set; } public bool localUIRight { get; set; } public string userVerifyMode { get; set; } public bool checkUser { get; set; } } public class CVaild { public bool enable { get; set; } public string beginTime { get; set; } public string endTime { get; set; } public string timeType { get; set; } } public class CRightPlan { public int doorNo { get; set; } public string planTemplateNo { get; set; } }
json字串為
{ "UserInfoSearch": { "searchID": "1","responseStatusStrg": "MORE","numOfMatches": 2,"totalMatches": 4,"UserInfo": [{ "employeeNo": "1","name": "管理員(131374","userType": "normal","closeDelayEnabled": false,"Valid": { "enable": false,"beginTime": "0-00-00T00:00:00","endTime": "0-00-00T00:00:00","timeType": "local" },"belongGroup": "","password": "","doorRight": "1","RightPlan": [{ "doorNo": 1,"planTemplateNo": "1" }],"maxOpenDoorTime": 0,"openDoorTime": 0,"roomNumber": 1,"floorNumber": 1,"localUIRight": false,"numOfCard": 0,"numOfFP": 0,"numOfFace": 0 },{ "employeeNo": "2","name": "123456","numOfFace": 1 }] } }
補充: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 = "",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\",\"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 |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。