大佬教程收集整理的这篇文章主要介绍了[翻译]Json.NET API-Linq to Json Basic Operator(基本操作),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在Json.NET开源的组件的api文档中看到其中有个Linq To Json基本操作.详细看了其中aPI 中Linq to sql命名空间下定义类方法.以及实现,觉得参与Linq 来操作Json从某种程度上提高生成Json字符窜的效率,特别对数据库中批量的数据. 但是也从侧面也增加程序员编码的难度(如果刚用不熟练情况下 主要是在编码中控制生成Json字符窜正确的格式),另外一个关键借助了Linq对Json数据操作和转换更加直接.Linq To sql 空间目的使用户利用Linq更加直接创建和查询Json对象. 翻译文档如下:
A:CreaTing Json-(利用Linq快速创建Json Object)
在Newtonsoft.Json.Linq 空间下有多个方法可以创建一个Json对象. 简单方法虽然能够创建,但是对编码而言较多略显累赘.简单创建代码如下:
1JArrayarray=newJArray(); 2JValuetext=newJValue("@H_377_38@manualtext"); 3JValuedate=newJValue(newdatetiR_368_11845@e(2000,5,23)); 4 5array.Add(text); 6array.Add(datE); 7 8Stringjson=array.ToString(); 10//生成的Json字符窜如下: 11//[ 12//"Manualtext", 13//"\/Date(958996800000+1200)\/" 14//] |
JArray是Newtonsoft.Json.Linq空间扩展的类表示一个Json数组.而JValue代表JSON值(字符串,整数,日期等) .
简单利用Linq To sql创建一个Json Object:
1List<Post>posts=GetPosts(); 2 3JObjectRSS= 4newJObject( 5newJProperty("chAnnel", 6newJObject( 7newJProperty("title","JamesNewton-King"), 8newJProperty("link","http://james.newtonking.com"), 9newJProperty("description","JamesNewton-King'sblog."), 10newJProperty("item", 11newJArray( 12frompinposts 13orderbyp.title 14SELEctnewJObject( 15newJProperty("title",p.titlE), 16newJProperty("description",p.Description), 17newJProperty("link",p.Link), 18newJProperty("category", 19newJArray( 20fromcinp.Categories 21SELEctnewJValue(C))))))))); 22 23Console.WriteLine(RSS.ToString()); 24//生成的Json字符窜如下: 25//{ 26//"chAnnel":{ 27//"title":"JamesNewton-King", 28//"link":"http://james.newtonking.com", 29//"description":"JamesNewton-King'sblog.", 30//"item":[ 31//{ 32//"title":"Json.NET1.3+Newlicense+NowonCodePlex", 33//"description":"AnnoucingthereleaSEOfJson.NET1.3,theMITlicenseandthesourcebeingavailabLeonCodePlex", 34//"link":"http://james.newtonking.com/projects/json-net.aspx", 35//"category":[ 36//"Json.NET", 37//"CodePlex" 38//] 39//}, 40//{ 41//"title":"LINQtoJSONbeta", 42//"description":"AnnoucingLINQtoJSON", 43//"link":"http://james.newtonking.com/projects/json-net.aspx", 44//"category":[ 45//"Json.NET", 46//"LINQ" 47//] 48//} 49//] 50//} 51//} |
分析一下: 如果按照普通方法把一个List集合生成Json对象字符窜.步骤如下: List首先从数据库中取出.然后利用JsonConvert实体类下的serializeObject()方法实例化才能返回Json字符窜. 相对而言Linq 直接操作数据库数据 一步到位 所以在编程效率上实现提高.
你可以FromObject()方法从一个非Json类型对象创建成Json Object.(自定义对象):
1JObjecto=JObject.FromObject(new 2{ 3chAnnel=new 4{ 5title="JamesNewton-King", 6link="http://james.newtonking.com", 7description="JamesNewton-King'sblog.", 8item= 9frompinposts 10orderbyp.title 11SELEctnew 12{ 13title=p.title, 14description=p.Description, 15link=p.Link, 16category=p.Categories 17} 18} 19}); |
最后可以通过Pares()方法把一个String字符窜创建一个Json对象:(手写控制Json格式):
1Stringjson=@"{ 2cpu:'Intel', 3Drives:[ 4'DVDread/writer', 5""500gigabyteharddrive"" 6] 7}"; 8 9JObjecto=JObject.Parse(json); |
当查询一个Json Object属性时最有用方法分别为:Children()方法和Property Index(属性索引),Children()方法将返回Json Object所有的Json子实体. 如果它是一个JObject将返回一个属性集合.如果是JArray返回一个数组值的集合. 但是Property Index用户获得特定的Children子实体.无论是JSON数组索引或JSON对象的属性名的位置.
1varposttitles= 2frompinRSS["chAnnel"]["item"].Children() 3SELEct(String)p["title"]; 4 5foreach(variteminposttitles) 6{ 7Console.WriteLine(item); 8} 9 10//LINQtoJSONbeta 11//Json.NET1.3+Newlicense+NowonCodePlex 12 13varcategories= 14fromcinRSS["chAnnel"]["item"].Children()["category"].Values<String>() 15groupcbycintog 16orderbyg.Count()descending 17SELEctnew{Category=g.Key,Count=g.Count()}; 18 19foreach(varcincategories) 20{ 21Console.WriteLine(c.Category+"-Count:"+c.Count); 22} 24//Json.NET-Count:2 25//LINQ-Count:1 26//CodePlex-Count:1 |
Linq to Json常常用于手动把一个Json Object转换成.NET对象 .
1publicclassShortie 2{ 3publicStringOriginal{get;set;} 4publicStringShortened{get;set;} 5publicStringShort{get;set;} 6publicShortieExceptionError{get;set;} 7} 8 9publicclassShortieException 10{ 11publicintCode{get;set;} 12publicStringErrormessage{get;set;} 13} |
手动之间的序列化和反序列化一个.NET对象是最常用情况是JSON Object和需要的。NET对象不匹配情况下.
1Stringjsontext=@"{ 2""short"":{ 3""original"":""http://www.foo.com/"", 4""short"":""krehqk"", 5""error"":{ 6""code"":0, 7""msg"":""Noactiontaken""} 8}"; 9 10JObjectjson=JObject.Parse(jsontext); 11 12Shortieshortie=newShortie 13{ 14Original=(String)json["short"]["original"], 15Short=(String)json["short"]["short"], 16Error=newShortieException 17{ 18Code=(int)json["short"]["error"]["code"], 19Errormessage=(String)json["short"]["error"]["@H_377_38@msg"] 20} 21}; 22 23Console.WriteLine(shortie.original); 24//http://www.foo.com/ 25 26Console.WriteLine(shortie.Error.ErrormessagE); 27//Noactiontaken |
个人翻译不是很好,最近主要是用的比较频繁. 今天总结一些基本用法.如想看原版的Linq To Json 编译 请参考官方地址下API,代码如果看不懂可以查看Newtonsoft.Json.Linq命名空间下定义类和集成静待方法或直接联系我.
以上是大佬教程为你收集整理的[翻译]Json.NET API-Linq to Json Basic Operator(基本操作)全部内容,希望文章能够帮你解决[翻译]Json.NET API-Linq to Json Basic Operator(基本操作)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。