欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# Newtonsoft.Json 的使用说明

时间:2022-08-02 09:20:14|栏目:.NET代码|点击:

我就废话不多说啦,大家还是直接看代码吧~

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 | 

上一篇:Unity实现识别图像中主体及其位置

栏    目:.NET代码

下一篇:Asp.Net Core使用Ocelot结合Consul实现服务注册和发现

本文标题:C# Newtonsoft.Json 的使用说明

本文地址:http://www.codeinn.net/misctech/209637.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有