C# Newtonsoft.Json 的使用說明
更新時(shí)間:2021年01月14日 08:34:10 作者:enych
這篇文章主要介紹了C# Newtonsoft.Json 的使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說啦,大家還是直接看代碼吧~
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();
//序列化這個(gè)字符串
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; } //查詢狀態(tài)字符串描述: OK-查詢結(jié)束, MORE-還有數(shù)據(jù)等待查詢, NO MATCH-沒有匹配數(shù)據(jù)
public int numOfMatches { get; set; } //本次返回的記錄條數(shù)
public int totalMatches { get; set; } //符合條件的記錄總條數(shù)
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
}]
}
}
補(bǔ)充:C#使用NewtonSoft操作Json實(shí)戰(zhàn)
上代碼~
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\":\"男\(zhòng)",\"age\":\"18\"}", user);
Console.WriteLine(user.id + " " + user.name + " " + user.sex + " " + user.age);
//輸出結(jié)果: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);
//輸出結(jié)果:[{"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+" | ");
}
//輸出結(jié)果:1 張三 男 18 | 2 李四 女 17 |
/
JArray jArrayUser = JArray.Parse("[{\"id\":\"1\",\"name\":\"張三\",\"sex\":\"男\(zhòng)",\"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() + " | ");
}
//輸出結(jié)果:1 張三 男 18 | 2 李四 女 17 |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:
- C#下Newtonsoft.Json的具體使用
- C# Newtonsoft.Json庫的常用屬性和方法詳解
- C#使用Newtonsoft.Json庫實(shí)現(xiàn)JSON數(shù)據(jù)中某個(gè)字段值的提取功能
- C# Newtonsoft.Json用法詳解
- C#使用Newtonsoft.Json中的JObject對象
- C# newtonsoft.json中文亂碼問號的解決方案
- c# Newtonsoft.Json 常用方法總結(jié)
- C# Newtonsoft.Json 解析多嵌套json 進(jìn)行反序列化的實(shí)例
- c#添加Newtonsoft.Json包的操作
- C#中Newtonsoft.Json 到 System.Text.Json 遷移避坑指南
相關(guān)文章
C#難點(diǎn)逐個(gè)擊破(8):可空類型System.Nullable
null值用來表示數(shù)據(jù)類型未被賦予任何值,它是一種引用類型;void表示沒有類型,或者說是沒有任何值。null與void的區(qū)別可以認(rèn)為void是根本沒有,而null是一個(gè)空箱子,里面什么都沒有。2010-02-02
C#實(shí)現(xiàn)壓縮和解壓縮的方法示例【Gzip和Zip方式】
這篇文章主要介紹了C#實(shí)現(xiàn)壓縮和解壓縮的方法,結(jié)合具體實(shí)例形式分析了Gzip和Zip兩種壓縮操作實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06
10分鐘學(xué)會(huì)VS NuGet包私有化部署
本文主要介紹了10分鐘學(xué)會(huì)VS NuGet包私有化部署,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Unity?UGUI的GraphicRaycaster射線投射組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的GraphicRaycaster射線投射組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

