资源预览内容
第1页 / 共17页
第2页 / 共17页
第3页 / 共17页
第4页 / 共17页
第5页 / 共17页
第6页 / 共17页
第7页 / 共17页
第8页 / 共17页
第9页 / 共17页
第10页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
C#中使用 JSON 不需要使用第三方库,使用.NET Framwork3.5 自带的 System.Runtime.Serialization.Json 即可很好的完成 JSON 的解析。一、Using需要添加引用:System.ServiceModel.Web 和 System.Runtime.Serialization,然后使用 Using:using System.Runtime.Serialization.Json;using System.Runtime.Serialization;二、定义序列化的类假如我们要转化的 JSON 字符串格式为:encoding:UTF-8,plug-ins:python,c+,ruby,indent:length:3,use_space:true然后编写相应的序列化的类,注意下面类加的 Attribute:DataContract(Namespace = http:/coderzh.cnblogs.com)class ConfigDataMember(Order = 0)public string encoding get; set; DataMember(Order = 1)public string plugins get; set; DataMember(Order = 2)public Indent indent get; set; DataContract(Namespace = http:/coderzh.cnblogs.com)class IndentDataMember(Order = 0)public int length get; set; DataMember(Order = 1)public bool use_space get; set; 三、对象转化为 JSON 字符串使用 WriteObject 方法:var config = new Config()encoding = UTF-8,plugins = new stringpython, C+, C#,indent = new Indent() length = 4, use_space= false;var serializer = new DataContractJsonSerializer(typeof(Config);var stream = new MemoryStream();serializer.WriteObject(stream, config);byte dataBytes = new bytestream.Length;stream.Position = 0;stream.Read(dataBytes, 0, (int)stream.Length);string dataString = Encoding.UTF8.GetString(dataBytes);Console.WriteLine(JSON string is:);Console.WriteLine(dataString);四、JSON 字符串转对象使用 ReadObject 方法: var mStream = new MemoryStream(Encoding.Default.GetBytes(dataString);Config readConfig = (Config)serializer.ReadObject(mStream);Console.WriteLine(Encoding is: 0, readConfig.encoding);foreach (string plugin in readConfig.plugins)Console.WriteLine(plugins is: 0, plugin);Console.WriteLine(indent.length is: 0, readConfig.indent.length);Console.WriteLine(indent.use_space is: 0, readConfig.indent.use_space);五、输出结果:JSON string is:encoding:UTF-8,plugins:python,C+,C#,indent:length:4,use_space:falseEncoding is: UTF-8plugins is: pythonplugins is: C+plugins is: C#indent.length is: 4indent.use_space is: Falsepublic List poisPoints = new List();/根据关键词检索某一城市区域内的信息,如检索北京市的的饭店public List SearchPlace(string keywords, string region)Uri uri = new Uri(http:/api.map.baidu.com/place/v2/search?&q= + keywords + ®ion= + region + /记住使用自己的akWebClient wc = new WebClient();wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);wc.OpenReadAsync(uri);return poisPoints;/重载SearchPlace方法,根据关键词检索某一矩形区域内的信息,矩形区域由左下角和右上角的经纬度坐标值定义lat为纬度值,lng为经度值 如39.915,116.404,39.975,116.414 public List SearchPlace(string keywords, string leftDownLat, string leftDownLng, string rightUpLat, string rightUpLng)Uri uri = new Uri(http:/api.map.baidu.com/place/v2/search?&query= + keywords + &bounds= + leftDownLat + , + leftDownLng + , + rightUpLat + , + rightUpLng + /记住使用自己的akWebClient wc = new WebClient();wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);wc.OpenReadAsync(uri);return poisPoints;/重载SearchPlace方法,根据关键词检索某一圆形区域内的信息,圆形区域由中心点的经纬度以及半径(单位为米)决定,半径最大为2000米public List SearchPlace(string keywords, string centerLat, string centerLng, string searchRadius)Uri uri = new Uri(http:/api.map.baidu.com/place/v2/search?&query= + keywords + &location= + centerLat + , + centerLng + &radius= + searchRadius + /记住使用自己的akWebClient wc = new WebClient();wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync(uri);return poisPoints;public void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SearchPlace);SearchPlace RC = (SearchPlace)serializer.ReadObject(e.Result);/ RestaurantClass为根据json数据结构创建的类foreach (SinglePlace sp in RC.results)string info = name: + sp.name + n + address= + sp.address + n + street_id= + sp.street_id + n + telephone= + sp.telephone + n + uid= + sp.uid + n +lng= + sp.location.lng + lat= + sp.location.lat + n +distance= + sp.detail_info.distance + type= + sp.detail_info.type + tag= + sp.detail_info.tag + detail_url= + sp.detail_info.detail_url + price= + sp.detail_info.price +shop_hours= + sp.detail_info.shop_hours + overall_rating= + sp.detail_info.overall_rating + hygiene_rating= + sp.detail_info.hygiene_rating + taste_rating= +sp.detail_info.taste_rating +service_rating= + sp.detail_info.service_rating + environment_rating= + sp.detail_info.environment_rating + facility_rating= + sp.detail_info.facility_rating +technology_rating= + sp.detail_info.technology_rating +image_num= + sp.detail_info.image_num + group_num= + sp.detail_info.groupon_num + discout_num= + sp.detail_info.groupon_num + comment_num= +sp.detail_info.comment_num + favorite_num= + sp.detail_info.comment_num+ checkin_num= + sp.detail_info.checkin_num;Graphic graphic = new Graphic();graphic.Geometry = new MapPoint(
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号