01 02 03 04 05 06 07 08 09 10 11 12 13 | // Newtonsoft.Json測(cè)試 private static void JsonTest() { // 創(chuàng)建一個(gè)字典 Dictionary< string , int > dic = new Dictionary< string , int > { { "James" , 9001 }, { "Jo" , 3474 }, { "Jess" , 11926 } }; // 將字典轉(zhuǎn)換成Json字符串 string json = JsonConvert.SerializeObject(dic); Console.WriteLine(json); } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | // Newtonsoft.Json測(cè)試 private static void JsonTest() { // 創(chuàng)建一個(gè)字典 Dictionary< string , int > dic = new Dictionary< string , int > { { "James" , 9001 }, { "Jo" , 3474 }, { "Jess" , 11926 } }; // 將字典轉(zhuǎn)換成Json字符串 string json = JsonConvert.SerializeObject(dic, Formatting.Indented); Console.WriteLine(json + "\n" ); // 創(chuàng)建一個(gè)新的字典保存結(jié)果 Dictionary< string , int > newDic = JsonConvert.DeserializeObject<Dictionary< string , int >>(json); // 打印字典中元素?cái)?shù)量 Console.WriteLine(newDic.Count); } |
01 02 03 04 05 06 07 08 09 10 11 | // Newtonsoft.Json測(cè)試 private static void JsonTest() { // 創(chuàng)建一個(gè)集合 List<List< string >> list = new List<List< string >>{ new List< string >{ "James" , "Jo" , "Jess" }, new List< string >{ "1001" , "1002" , "1003" } }; // 將集合轉(zhuǎn)換成Json字符串 string json = JsonConvert.SerializeObject(list, Formatting.Indented); Console.WriteLine(json); } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | // Newtonsoft.Json測(cè)試 private static void JsonTest() { // 創(chuàng)建一個(gè)集合 List<List< string >> list = new List<List< string >>{ new List< string >{ "James" , "Jo" , "Jess" }, new List< string >{ "1001" , "1002" , "1003" } }; // 將集合轉(zhuǎn)換成Json字符串 string json = JsonConvert.SerializeObject(list, Formatting.Indented); Console.WriteLine(json + "\n" ); // 創(chuàng)建一個(gè)新集合儲(chǔ)存結(jié)果 List<List< string >> newList = JsonConvert.DeserializeObject<List<List< string >>>(json); foreach (List< string > child0 in newList) { foreach ( string child1 in child0) { Console.WriteLine(child1); } } } |
聯(lián)系客服