C\#Newtonsoft.JSon 의 사용 설명

9564 단어 C#NewtonsoftJson
긴 말 안 할 게 요.그냥 코드 보 세 요~

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 | 
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.만약 잘못 이 있 거나 완전히 고려 하지 않 은 부분 이 있다 면 아낌없이 가르침 을 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기