日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# Newtonsoft.Json 应用

發布時間:2024/4/15 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# Newtonsoft.Json 应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

常用的一個簡單方法

?

1 string jsonText = "{\"zone\":\"海淀\",\"zone_en\":\"haidian\"}"; 2 JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); 3 string zone = jo["zone"].ToString(); 4 string zone_en = jo["zone_en"].ToString();

?

?

?

?

?

通過JArray和JObject來創建一個音樂專輯結構的一個示例:

?

1 //Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject {{"Entered", DateTime.Now}}; 2 3 Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject(); 4 5 jsonObject.Add("Entered", DateTime.Now); 6 7 dynamic album = jsonObject; 8 9 album.AlbumName = "Dirty Deeds Done Dirt Cheap"; 10 album.Artist = "AC/DC/DVD"; 11 album.YearReleased = DateTime.Now.Year; 12 13 album.Songs = new Newtonsoft.Json.Linq.JArray() as dynamic; 14 15 dynamic song = new Newtonsoft.Json.Linq.JObject(); 16 song.SongName = "Dirty Deeds Done Dirt Cheap"; 17 song.SongLength = "4:05"; 18 album.Songs.Add(song); 19 20 song = new Newtonsoft.Json.Linq.JObject(); 21 song.SongName = "Love at First Feel"; 22 song.SongLength = "3:01"; 23 album.Songs.Add(song); 24 25 song = new Newtonsoft.Json.Linq.JObject(); 26 song.SongName = "小蘋果"; 27 song.SongLength = "03:32"; 28 album.Songs.Add(song); 29 30 Console.WriteLine(album.ToString()); 31 32 Console.ReadLine();

?

?

?

?

以上代碼最重要的是沒有明確指定類型,便可將動態對象進行JSON序列化,而JObject對象只是接收數據,具體結構通過動態語言在運行時生成,這意味著此代碼可以在運行時被編譯,從而體現動態語言的優勢。序列化的結果如下圖所示:

?

?

?

?

1 //Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject {{"Entered", DateTime.Now}}; 2 Newtonsoft.Json.Linq.JObject jsonObject = new Newtonsoft.Json.Linq.JObject(); 3 jsonObject.Add("Entered", DateTime.Now); 4 dynamic album = jsonObject; 5 6 album.AlbumName = "非主流歌曲"; 7 8 foreach (var item in jsonObject) //循環輸出動態的值 JObject(基類為JContainer、JObject和JArray)是一個集合,實現了IEnumerable接口,因此你還可以輕松地在運行時循環訪問 9 { 10 Console.WriteLine(item.Key + "的值為:" + item.Value.ToString()); 11 }

?

?

?

?

JObject.Parse()和JArray.Parse()方法導入JSON格式,JToken結構支持Parse()和Load()方法,這兩個方法可以分別從字符串或各種流讀取JSON數據。JValue包括最核心的JSON 解析能力,支持將字符串轉化為我們熟悉的動態對象。將JSON字符串轉換為成JObject對象,并強制轉換為動態類型。

?

1 var jsonString = @"{""Name"":""小蘋果"",""Company"":""韓國公司"", ""Entered"":""2016-11-26 00:14""}"; 2 3 dynamic json = Newtonsoft.Json.Linq.JToken.Parse(jsonString) as dynamic; 4 5 string name = json.Name; 6 string company = json.Company; 7 DateTime entered = json.Entered; 8 Console.WriteLine("name:" + name); 9 Console.WriteLine("company:" + company); 10 Console.WriteLine("entered:" + entered); 11 12 Console.ReadLine();

?

?

?

?

將JObject和JArray實例映射到一個強類型的對象,所以你可以在同一段代碼中混合書寫動態和靜態類型

?

1 string jsonString1 = @"[{""Name"":""小蘋果"",""Age"":""20""},{""Name"":""演員"",""Age"":""2""}]"; 2 Newtonsoft.Json.Linq.JArray userAarray1 = Newtonsoft.Json.Linq.JArray.Parse(jsonString1) as Newtonsoft.Json.Linq.JArray; 3 List<User> userListModel = userAarray1.ToObject<List<User>>(); 4 foreach (var userModel1 in userListModel) 5 { 6 Console.WriteLine("Name:" + userModel1.Name); 7 Console.WriteLine("Age:" + userModel1.Age); 8 } 9 10 Console.WriteLine(""); 11 string jsonString = @"[{""Name"":""小蘋果"",""Age"":""20""}]"; 12 Newtonsoft.Json.Linq.JArray userAarray = Newtonsoft.Json.Linq.JArray.Parse(jsonString) as Newtonsoft.Json.Linq.JArray; 13 Newtonsoft.Json.Linq.JObject jObject = userAarray[0] as Newtonsoft.Json.Linq.JObject; 14 User userModel = jObject.ToObject<User>(); 15 Console.WriteLine("Name:" + userModel.Name); 16 Console.WriteLine("Age:" + userModel.Age);

?

?

1 public class User 2 { 3 public string Name { set; get; } 4 public int Age { set; get; } 5 }

?

?

?

?

JSON.NET對動態語言的支持,但也別忘了它對靜態類型的強大支持,關于如何序列化和反序列化強類型對象,JsonConvert是一個高級別的靜態類,包裝更低級別的功能,但你也可以使用JsonSerializer類,該類可以序列化和反序列化各種流

?

1 UserType album = new UserType() 2 { 3 Type = "普通用戶", 4 UserListModel = new List<User>() 5 { 6 new User() 7 { 8 Name="張三", 9 Age = 20 10 }, 11 new User() 12 { 13 Name="李四", 14 Age = 30 15 } 16 } 17 }; 18 19 // serialize to string 20 string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(album, Newtonsoft.Json.Formatting.Indented); 21 Console.WriteLine("序列化結果"); 22 Console.WriteLine(""); 23 Console.WriteLine(json2); 24 25 UserType userType = Newtonsoft.Json.JsonConvert.DeserializeObject<UserType>(json2); 26 Console.WriteLine(""); 27 Console.WriteLine("反序列化:"); 28 Console.WriteLine("Type:"+ userType.Type); 29 Console.WriteLine(""); 30 foreach (var userModel in userType.UserListModel) 31 { 32 Console.WriteLine("Name:"+userModel.Name); 33 Console.WriteLine("Age:" + userModel.Age); 34 }

?

?

1 public class UserType 2 { 3 public string Type { get; set; } 4 public List<User> UserListModel { get; set; } 5 } 6 7 public class User 8 { 9 public string Name { set; get; } 10 public int Age { set; get; } 11 }

?

?

?

?

?

?轉自? ??http://www.cnblogs.com/linJie1930906722/p/6103455.html

轉載于:https://www.cnblogs.com/cwmizlp/p/9451103.html

總結

以上是生活随笔為你收集整理的C# Newtonsoft.Json 应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。