在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在工作中和手机通信用到web服务和javascriptSerializer,返回json数据,供手机端调用,一开始返回的数据是一大堆,比如 [{"word_picture9":"http://boss.xbxw.net/Upload/word/guojia/p_yinggelan.jpg","word_picture8":"http://boss.xbxw.net/Upload/word/guojia/p_zhongguo.jpg","word_voice2":"http://boss.xbxw.net/Upload/voice/approve.mp3","word_picture13":"http://boss.xbxw.net/Upload/word/gaokao/p_qingxu.jpg","word_voice14":"http://boss.xbxw.net/Upload/voice/Laos.mp3","word_picture10":"http://boss.xbxw.net/Upload/word/jijie/p_chuntian.jpg","word_voice11":"http://boss.xbxw.net/Upload/voice/Tidy.mp3","word_voice1":"http://boss.xbxw.net/Upload/voice/Mexico.mp3","word_picture15":"http://boss.xbxw.net/Upload/word/gaokao/p_dizhixue.jpg","word_picture16":"http://boss.xbxw.net/Upload/word/gaokao/p_youhuo.jpg","word_voice17":"http://boss.xbxw.net/Upload/voice/Austria.mp3","word_voice10":"http://boss.xbxw.net/Upload/voice/Spring.mp3","word_voice3":"http://boss.xbxw.net/Upload/voice/Giant.mp3","word_voice0":"http://boss.xbxw.net/Upload/voice/Fertilizer.mp3","word_picture12":"http://boss.xbxw.net/Upload/word/jiaotong/p_qianshuiting.jpg","word_picture17":"http://boss.xbxw.net/Upload/word/guojia/p_aodili.jpg","word_voice16":"http://boss.xbxw.net/Upload/voice/Temptation.mp3","word_voice6":"http://boss.xbxw.net/Upload/voice/Oxygen.mp3","word_voice12":"http://boss.xbxw.net/Upload/voice/submarine.mp3","word_voice13":"http://boss.xbxw.net/Upload/voice/mood.mp3","word_voice4":"http://boss.xbxw.net/Upload/voice/Volunteer.mp3","word_picture14":"http://boss.xbxw.net/Upload/word/guojia/p_laowo.jpg","word_voice5":"http://boss.xbxw.net/Upload/voice/label.mp3","word_voice8":"http://boss.xbxw.net/Upload/voice/china.mp3","word_voice9":"http://boss.xbxw.net/Upload/voice/England.mp3","word_picture11":"http://boss.xbxw.net/Upload/word/gaokao/p_zhegnjiede.jpg","word_voice7":"http://boss.xbxw.net/Upload/voice/Dusk.mp3","word_picture5":"http://boss.xbxw.net/Upload/word/gaokao/p_biaoqian.jpg","word_picture4":"http://boss.xbxw.net/Upload/word/gaokao/p_zhiyuanzhe.jpg","word_picture7":"http://boss.xbxw.net/Upload/word/gaokao/p_huanghun.jpg","word_picture6":"http://boss.xbxw.net/Upload/word/gaokao/p_yangqi.jpg","word_picture1":"http://boss.xbxw.net/Upload/word/guojia/p_moxige.jpg","word_picture0":"http://boss.xbxw.net/Upload/word/gaokao/p_feiliao.jpg","word_picture3":"http://boss.xbxw.net/Upload/word/gaokao/p_juren.jpg","word_picture2":"http://boss.xbxw.net/Upload/word/gaokao/p_pizhun.jpg","word_voice15":"http://boss.xbxw.net/Upload/voice/Geology.mp3"}]
用的方法是
///这个方法是得到单词相关信息
[WebMethod] JavaScriptSerializer ser = new JavaScriptSerializer(); 通过下面原作者得到的结果看得到的数据应该是[{"name":"wanwan","age:65"},{"name":"xiaohua","age:25"}],这样的形式,而不是一个中括号,一个大括号了事,经过对上面的观察结合下面的文章,发现一个对象代表一条数据,所以,上面的应该改成 Hashtable ht = null; eventList.Add(ht); 这样,每次循环一次,new一个对象,然后对象添加到list集合里,循环玩后list集合里就集中了很多hashtable对象,每一个对象将来就是一个大括号里的数据,序列化完事后就是下面的样式
[{"word_picture0":"http://boss.xbxw.net/Upload/word/yueqi/p_xiaotiqin.jpg","word_voice0":"http://boss.xbxw.net/Upload/voice/violin.mp3"},{"word_voice1":"http://boss.xbxw.net/Upload/voice/drum.mp3","word_picture1":"http://boss.xbxw.net/Upload/word/yueqi/p_gu.jpg"},{"word_voice2":"http://boss.xbxw.net/Upload/voice/shovel.mp3","word_picture2":"http://boss.xbxw.net/Upload/word/shenghuo/p_chanzi.jpg"},{"word_picture3":"http://boss.xbxw.net/Upload/word/gaokao/p_fenmo.jpg","word_voice3":"http://boss.xbxw.net/Upload/voice/Powder.mp3"},{"word_voice4":"http://boss.xbxw.net/Upload/voice/Media.mp3","word_picture4":"http://boss.xbxw.net/Upload/word/gaokao/p_duomeiti.jpg"},{"word_picture5":"http://boss.xbxw.net/Upload/word/gaokao/p_luxianhangxian.jpg","word_voice5":"http://boss.xbxw.net/Upload/voice/Route.mp3"},{"word_picture6":"http://boss.xbxw.net/Upload/word/gaokao/p_wangluo.jpg","word_voice6":"http://boss.xbxw.net/Upload/voice/network.mp3"},{"word_picture7":"http://boss.xbxw.net/Upload/word/gaokao/p_guzhang.jpg","word_voice7":"http://boss.xbxw.net/Upload/voice/Applause.mp3"},{"word_voice8":"http://boss.xbxw.net/Upload/voice/Liberia.mp3","word_picture8":"http://boss.xbxw.net/Upload/word/guojia/p_libiliya.jpg"},{"word_picture9":"http://boss.xbxw.net/Upload/word/gaokao/p_qiyou.jpg","word_voice9":"http://boss.xbxw.net/Upload/voice/petrol.mp3"},{"word_voice10":"http://boss.xbxw.net/Upload/voice/Nylon.mp3","word_picture10":"http://boss.xbxw.net/Upload/word/gaokao/p_nilong.jpg"},{"word_picture11":"http://boss.xbxw.net/Upload/word/jiaotong/p_kache.jpg","word_voice11":"http://boss.xbxw.net/Upload/voice/Truck.mp3"},{"word_picture12":"http://boss.xbxw.net/Upload/word/guojia/p_baxi.jpg","word_voice12":"http://boss.xbxw.net/Upload/voice/Brazil.mp3"},{"word_voice13":"http://boss.xbxw.net/Upload/voice/sketch.mp3","word_picture13":"http://boss.xbxw.net/Upload/word/gaokao/p_sumiao.jpg"},{"word_picture14":"http://boss.xbxw.net/Upload/word/dianzi/p_shubiao.jpg","word_voice14":"http://boss.xbxw.net/Upload/voice/mouse.mp3"},{"word_voice15":"http://boss.xbxw.net/Upload/voice/deposit.mp3","word_picture15":"http://boss.xbxw.net/Upload/word/gaokao/p_cunkuan.jpg"},{"word_picture16":"http://boss.xbxw.net/Upload/word/jiaotong/p_tuolaji.jpg","word_voice16":"http://boss.xbxw.net/Upload/voice/tractor.mp3"},{"word_picture17":"http://boss.xbxw.net/Upload/word/jiaotong/p_silunmache.jpg","word_voice17":"http://boss.xbxw.net/Upload/voice/wagon.mp3"}]
下面是原来作者的 命名空间: ASP.NET中的JSON序列化和反序列化还可以使用JavaScriptSerializer,在System.Web.Script.Serializatioin命名空间下,需引用System.Web.Extensions.dll.
实例: class People 序列化: List<People> list = new List<People>(); People peo2 = new People(); JavaScriptSerializer ser = new JavaScriptSerializer(); TextBox1.Text = ser.Serialize(list); 显示结果: [ {"Id":1,"Name":"zhangsan","Age":"\/Date(1363336335448)\/"}, {"Id":2,"Name":"lisi","Age":"\/Date(1363336335448)\/"} ]
反序列化: string json = TextBox1.Text; JavaScriptSerializer ser=new JavaScriptSerializer(); List<People> list = ser.Deserialize<List<People>>(json); foreach (People peo in list)
前台脚本解析: $(":submit:eq(2)").click(function () { |
请发表评论