JSON

.NetʹÓÃNewtonsoft.Json.dll(JSON.NET)¶ÔÏóÐòÁл¯³Éjson¡¢·´Ðò

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-05-22 13:05 ÎÒÒªÆÀÂÛ( )

JSON×÷ΪһÖÖÇáÁ¿¼¶µÄÊý¾Ý½»»»¸ñʽ£¬¼òµ¥Áé»î£¬±»ºÜ¶àϵͳÓÃÀ´Êý¾Ý½»»¥£¬×÷ΪһÃû.NET¿ª·¢ÈËÔ±£¬JSON.NETÎÞÒÉÊÇ×îºÃµÄÐòÁл¯¿ò¼Ü£¬Ö§³ÖXMLºÍJSONÐòÁл¯£¬¸ßÐÔÄÜ

JSON×÷ΪһÖÖÇáÁ¿¼¶µÄÊý¾Ý½»»»¸ñʽ£¬¼òµ¥Áé»î£¬±»ºÜ¶àϵͳÓÃÀ´Êý¾Ý½»»¥£¬×÷ΪһÃû.NET¿ª·¢ÈËÔ±£¬JSON.NETÎÞÒÉÊÇ×îºÃµÄÐòÁл¯¿ò¼Ü£¬Ö§³ÖXMLºÍJSONÐòÁл¯£¬¸ßÐÔÄÜ£¬Ãâ·Ñ¿ªÔ´£¬Ö§³ÖLINQ²éѯ¡£Ä¿Ç°Òѱ»Î¢Èí¼¯³ÉÓÚwebapi¿ò¼ÜÖ®ÖУ¬Òò´Ë£¬ÊìÁ·ÕÆÎÕJSON.NETÏ൱ÖØÒª£¬ÕâƪÎÄÕÂÊÇÁã¶È²Î¿¼¹ÙÍøÕûÀíµÄʾÀý£¬Í¨¹ýÕâЩʾÀý£¬¿ÉÒÔÈ«ÃæÁ˽âJSON.NETÌṩµÄ¹¦ÄÜ¡£

Newtonsoft.JsonµÄµØÖ·£º

¹ÙÍø£º

Ô´ÂëµØÖ·£ºhttps://github.com/JamesNK/Newtonsoft.Json

Newtonsoft.Json.dllÏÂÔØ£ºhttps://github.com/JamesNK/Newtonsoft.Json/releases

1¡¢Ê¹ÓÃNewtonsoft.Json(JSON.NET)ÐòÁл¯¶ÔÏó,ͨ¹ýNewtonsoft.Json.Formatting½«json¸ñʽ»¯Êä³ö¡£

Account account = new Account { Email = , Active = true, CreatedDate =DateTime.Now, Roles = , } }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(json); public class Account { public string Name { get; set; } public string Email { get; set; } public bool Active { get; set; } public DateTime CreatedDate { get; set; } public IList<string> Roles { get; set; } }

Ö´Ðнá¹û£º

2¡¢Ê¹ÓÃNewtonsoft.Json(JSON.NET)ÐòÁл¯List¼¯ºÏ£º

List<, , ,, }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(videogames); Console.WriteLine(json);

Ö´Ðнá¹û£º

3¡¢Ê¹ÓÃNewtonsoft.Json(JSON.NET)ÐòÁл¯dictionary×Öµä

System.Collections.Generic.Dictionary<string, string> dic = new System.Collections.Generic.Dictionary<string, string> { { , }, { , }, { , } }; string json1 = Newtonsoft.Json.JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented); Console.WriteLine(json1); Console.WriteLine(""); Console.WriteLine(); string json2 = Newtonsoft.Json.JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.None); Console.WriteLine(json2);

Ö´Ðнá¹û£º

4¡¢Newtonsoft.Json(JSON.NET)½«ÐòÁл¯½á¹û±£´æµ½Ö¸¶¨µÄÎļþ£º

User movie = , Age = 1993 }; )) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(file, movie); } public class User { public string Name { set; get; } public int Age { set; get; } }

Ö´Ðк󱣴浽ÎļþµÄ½á¹û£º

5¡¢Newtonsoft.Json(JSON.NET)»ùÓÚö¾ÙÀàÐ͵ÄJsonConvertersת»»Æ÷

List<JosnEnum> list = new List<JosnEnum> { JosnEnum.NotStartus, JosnEnum.Startus }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(list); Console.WriteLine(json); Console.WriteLine(""); System.Collections.Generic.Dictionary<string, int> dic = new System.Collections.Generic.Dictionary<string, int> { {((JosnEnum)(int)JosnEnum.NotStartus).ToString() ,(int)JosnEnum.NotStartus} , {((JosnEnum)(int)JosnEnum.Startus).ToString() ,(int)JosnEnum.Startus} }; string dicJson = Newtonsoft.Json.JsonConvert.SerializeObject(dic); Console.WriteLine(dicJson); Console.WriteLine(""); List<JosnEnum> list2 = new List<JosnEnum> { JosnEnum.NotStartus, JosnEnum.Startus }; string json3 = Newtonsoft.Json.JsonConvert.SerializeObject(list2, new Newtonsoft.Json.Converters.StringEnumConverter()); Console.WriteLine(json3); Console.WriteLine(""); List<JosnEnum> result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JosnEnum>>(json3, new Newtonsoft.Json.Converters.StringEnumConverter()); Console.WriteLine(, result.Select(c => c.ToString()))); public enum JosnEnum { NotStartus = 0, Startus = 1 }

Ö´Ðнá¹û£º

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
ÍøÓѵãÆÀ
Æ