|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
在经过全球个人PC市场占有90%的微软对asp.net不断优化与整合后,asp.net与微软自身平台的动用上更加的高效,加上asp.net在应用上非常容易上手,相信asp.net仍会是最多客户选用的脚本语言,并会在未来几年继续领跑。因为比来较忙,上一篇的坑估量要渐渐填了。感到写文章先容要比写程序还累啊。先看看测试程序,可以懂得api撑持哪些功效:
- staticvoidMain(string[]args){//wrapper对象dynamicdata=DJson.Wrap(new{name="Jane",male=false,age=24,dob=DateTime.Now,friend=new{name="Jesse",male=true,age=32,dob=DateTime.Now},mobile=newobject[]{new[]{86},13888888888},});Console.WriteLine(data.Stringfy());//数组data=newDJsonArray();data[0]=new{name="小朋盂1号"};data[1]=new{name="小朋盂2号"};data[2]=new{name="小朋盂3号"};Console.WriteLine(data.Stringfy());//反序列化varjson=data.Stringfy();dynamicobj=DJson.Parse(json);//修正反序列化后的数据obj[1]=new{name="mike",age=21,dob=DateTime.Now};Console.WriteLine(obj.Stringfy());//实践使用,将twitterstatues反序列化json=System.IO.File.ReadAllText("e:/twitter.js");dynamicstatuses=DJson.Parse(json);for(inti=0;i<statuses.Length;i++){varstatus=statuses[i];Console.WriteLine("id:{0}nname:{1}nfrom:{2}ntext:{3}n",status.user.id,status.user.name,status.user.location,status.text);}Console.ReadKey();}
复制代码
twitter文件请在这里下载
先放上代码,实在很复杂,今后再渐渐剖析
- publicclassDJson:DynamicObject{protectedDictionary<string,object>_expando=newDictionary<string,object>();publicoverridestringToString(){returnthis.Stringify();}publicIEnumerable<string>Keys{get{return_expando.Keys;}}publicobjectthis[stringkey]{get{return_expando[key];}set{_expando[key]=value;}}publicDJson(){}publicstaticDJsonWrap(objectobj){if(!(objisstring)&&(objisIEnumerable)&&!(objisDictionary<string,object>)){vararray=newDJsonArray();inti=0;foreach(objectvinobjasIEnumerable)array[i++]=DJson.Wrap(v);returnarray;}else{vardj=newDJson();if(objisDictionary<string,object>){foreach(varkvinobjasDictionary<string,object>){if(kv.Value==null)dj[kv.Key]=(object)null;elseif(IsPrimitive(kv.Value.GetType()))dj[kv.Key]=kv.Value;elsedj[kv.Key]=DJson.Wrap(kv.Value);}}else{vart=obj.GetType();if(IsPrimitive(t))thrownewArgumentException();foreach(varpint.GetProperties()){varv=p.GetValue(obj,null);if(IsPrimitive(p.PropertyType))dj[p.Name]=v;elsedj[p.Name]=DJson.Wrap(v);}}returndj;}}publicstaticDJsonParse(stringjson){varobj=(newJavaScriptSerializer()).DeserializeObject(json);returnDJson.Wrap(obj);}publicstringStringify(){varserializer=newJavaScriptSerializer();serializer.RegisterConverters(newJavaScriptConverter[]{newDJsonConverter()});if(thisisDJsonArray)returnserializer.Serialize((thisasDJsonArray).ToArray());returnserializer.Serialize(this);}publicoverrideboolTryGetMember(GetMemberBinderbinder,outobjectresult){if(_expando.TryGetValue(binder.Name,outresult))returntrue;returnfalse;}publicoverrideboolTrySetMember(SetMemberBinderbinder,objectvalue){if(!(valueisstring)&&(valueisIEnumerable))_expando[binder.Name]=newDJsonArray(valueasIEnumerable);else_expando[binder.Name]=value;returntrue;}internalstaticboolIsPrimitive(Typetype){returntype.FullName.StartsWith("System.")&&!type.FullName.StartsWith("System.Collection");}}publicclassDJsonArray:DJson{Dictionary<int,object>_array=newDictionary<int,object>();publicobjectthis[intindex]{get{return_array[index];}set{_array[index]=value;}}publicintLength{get{return_array.Keys.Count;}}publicDJsonArray(){}publicDJsonArray(IEnumerablearray){inti=0;foreach(varobjinarray)this[i++]=obj;}publicobject[]ToArray(){return_array.Values.ToArray();}publicDJsonToObject(){vardj=newDJson();foreach(stringkeyin_expando.Keys)dj[key]=_expando[key];foreach(intkeyin_array.Keys)dj[key.ToString()]=_array[key];returndj;}}internalclassDJsonConverter:JavaScriptConverter{publicoverrideIDictionary<string,object>Serialize(objectobj,JavaScriptSerializerserializer){vardj=(DJson)obj;varresult=newDictionary<string,object>();foreach(stringkeyindj.Keys){if(dj[key]isDJsonArray)result[key]=(dj[key]asDJsonArray).ToArray();elseresult[key]=dj[key];}returnresult;}publicoverrideIEnumerable<Type>SupportedTypes{get{returnnewList<Type>(newType[]{typeof(DJson),typeof(DJsonArray)});}}publicoverrideobjectDeserialize(IDictionary<string,object>dictionary,Typetype,JavaScriptSerializerserializer){thrownewNotImplementedException();}}
复制代码
Newton的json库比来仿佛做了不异的事变,入手下手撑持dynamic了
http://james.newtonking.com/archive/2011/01/03/json-net-4-0-release-1-net-4-and-windows-phone-support.aspx
语言是不是不是最重要的? |
|