国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
【新提醒】【小賤學(xué)C#筆記之Newtonsoft.Json(三)】

本帖最后由 佑丶小賤 于 2015-10-22 11:31 編輯

        今天小賤繼續(xù)講Newtonsoft.Json的使用。之前講了Json與Xml、類(lèi)的互相轉(zhuǎn)換,當(dāng)然功能不僅僅只有這些,下面就提到另幾個(gè)轉(zhuǎn)換。下面的例子都需要引用Newtonsoft.Json這個(gè)庫(kù)。
[C#] 純文本查看 復(fù)制代碼
using Newtonsoft.Json; // Newtonsoft.Json引用

1.將字典轉(zhuǎn)換成Json字符串
[C#] 純文本查看 復(fù)制代碼
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);
        }

我們看下運(yùn)行的結(jié)果:



        是不是很簡(jiǎn)單?如果看過(guò)我之前一篇文章的朋友,是不是有似曾相識(shí)的感覺(jué)?沒(méi)錯(cuò),用的是同一個(gè)函數(shù)JsonConvert.SerializeObject(),接下我們還會(huì)用到它,現(xiàn)在先放著。我們說(shuō)說(shuō)這個(gè)函數(shù)的另一個(gè)參數(shù)Formatting,也就是Json字符串顯示的格式。我們?cè)诤瘮?shù)中添加一個(gè)參數(shù)Formatting.Indented,看看結(jié)果又什么不同。



是不是比上面的結(jié)果更加直觀了?這個(gè)用法在要將Json字符串記錄到文檔中十分有效。

2.Json字符串轉(zhuǎn)換成字典
[C#] 純文本查看 復(fù)制代碼
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);
        }

我們將前面的代碼修改一下,之前得到的Json作為參數(shù)使用。來(lái)觀察下結(jié)果:




3.集合轉(zhuǎn)換成Json字符串
[C#] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
// Newtonsoft.Json測(cè)試
        private static void JsonTest()
        {
            // 創(chuàng)建一個(gè)集合
            List<string> list = new List<string>(){
                "James",
                "Jo",
                "Jess"
            };
            // 將集合轉(zhuǎn)換成Json字符串
            string json = JsonConvert.SerializeObject(list, Formatting.Indented);
            Console.WriteLine(json);
        }





這是一個(gè)一階的集合,如果是多階的呢?我們來(lái)試試:
[C#] 純文本查看 復(fù)制代碼
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);
        }





看來(lái)也是可以實(shí)現(xiàn)的



4.Json字符串轉(zhuǎn)換成集合
[C#] 純文本查看 復(fù)制代碼
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);
                }
            }
        }

還是用前一個(gè)實(shí)驗(yàn)的結(jié)果來(lái)作這個(gè)實(shí)驗(yàn)的參數(shù),然后遍歷出集合中的結(jié)果。










OK啦!超方便的吧!今天的筆記OVER,吃飯吃飯!
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
.Net使用Newtonsoft.Json.dll(JSON.NET)對(duì)象序列化成json、反序列化json示例教程
【轉(zhuǎn)】在C#中使用Json.Net進(jìn)行序列化和反序列化及定制化
在.NET中使用Newtonsoft.Json轉(zhuǎn)換,讀取,寫(xiě)入
C# 操作JSON的幾種方式
C# 后臺(tái)解析json,簡(jiǎn)單方法 字符串序列化為對(duì)象,取值
Json.Net系列教程 3.Json.Net序列化和反序列化設(shè)置
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服