概述
我就废话不多说啦,大家还是直接看代码吧~
byte[] bUserInfoSearch = new byte[1024 * 10]; //10kb大小 Marshal.Copy(lpBuffer, bUserInfoSearch, 0, 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", "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": 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 = "", 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 的使用说明的全部内容,希望文章能够帮你解决C# Newtonsoft.Json 的使用说明所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复