博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform
阅读量:6209 次
发布时间:2019-06-21

本文共 9999 字,大约阅读时间需要 33 分钟。

///     /// http://www.weather.com.cn/data/sk/101280601.html    ///  {"weatherinfo":{"city":"深圳","cityid":"101280601","temp":"32","WD":"西南风","WS":"4级","SD":"68%","WSE":"4","time":"16:40","isRadar":"1","Radar":"JC_RADAR_AZ9755_JB"}}    /// 20140531 涂聚文 Geovin Du    ///     public class WeatherInfo     {        //public  string city;        //public string cityid;        //public string temp;        //public string WD;        //public string WS;        //public string SD;        //public string WSE;        //public string time;        //public string isRadar;        //public string Radar;        string _city;        string _cityid;        string _temp;        string _WD;        string _WS;        string _SD;        string _WSE;        string _time;        string _isRadar;        string _Radar;        ///         /// 城市名称        ///         public string city        {            get            {                return _city;            }            set            {                _city = value;            }        }        ///         /// 城市代码        ///         public string cityid        {            get            {                return _cityid;            }            set            {                _cityid = value;            }            //get;            //set;        }        ///         /// 温度        ///         public string temp        {            get            {                return _temp;            }            set            {                _temp = value;            }        }        ///         /// 风向        ///         public string WD        {            get            {                return _WD;            }            set            {                _WD = value;            }              }        ///         /// 风级        ///         public string WS        {            get            {                return _WS;            }            set            {                _WS = value;            }              }        ///         /// 湿度        ///         public string SD        {            get            {                return _SD;            }            set            {                _SD = value;            }             }        ///         ///         ///         public string WSE        {            get            {                return _WSE;            }            set            {                _WSE = value;            }        }        ///         /// 发布时间        ///         public string time        {            get            {                return _time;            }            set            {                _time = value;            }        }        ///         ///         ///         public string isRadar        {            get            {                return _isRadar;            }            set            {                _isRadar = value;            }              }        ///         ///         ///         public string Radar        {            get            {                return _Radar;            }            set            {                _Radar = value;            }                }        private Dictionary
_theRest = new Dictionary
(); public Dictionary
TheRest { get { return _theRest; } } // public Dictionary
Rates { get; set; } }
///     /// http://www.weather.com.cn/data/sk/101280601.html    /// 20140531 涂聚文 Geovin Du    ///     public class WeatherInfoConverter : CustomCreationConverter
{ public override WeatherInfo Create(Type objectType) { return new WeatherInfo(); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { WeatherInfo mappedObj = new WeatherInfo(); //get an array of the object's props so I can check if the JSON prop s/b mapped to it PropertyInfo[] myPropertyInfo; myPropertyInfo = objectType.GetProperties(); string objProps = string.Empty; //for (int i = 0; i < myPropertyInfo.Length; i++) //{ // objProps = objProps +" "+ myPropertyInfo[i].ToString(); //} foreach (PropertyInfo pi in objectType.GetProperties()) { object value1 = pi.GetValue(mappedObj, null);//用pi.GetValue获得值 objProps = objProps + " " + pi.Name;//获得属性的名字,后面就可以根据名字判断来进行些自己想要的操作 //获得属性的类型,进行判断然后进行以后的操作,例如判断获得的属性是整数 //if(value1.GetType() == typeof(int)) //{ // //进行你想要的操作 //} } //objProps = myPropertyInfo[0].Name.ToLower().ToString(); //objectType.GetProperties().Select(p => p.Name.ToLower()).ToArray(); //loop through my JSON string while (reader.Read()) { //if I'm at a property... if (reader.TokenType == JsonToken.PropertyName) { //convert the property to lower case string readerValue = reader.Value.ToString().ToLower(); if (reader.Read()) //read in the prop value { //is this a mapped prop? if (objProps.Contains(readerValue)) { //get the property info and set the Mapped object's property value PropertyInfo pi = mappedObj.GetType().GetProperty(readerValue, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); object convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType); pi.SetValue(mappedObj, convertedValue, null); } else { //otherwise, stuff it into the Dictionary mappedObj.TheRest.Add(readerValue, reader.Value); } } } } return mappedObj; } }

 

///     /// http://www.weather.com.cn/data/sk/101280601.html    /// http://www.weather.com.cn/data/cityinfo/101280601.html    /// http://geoip.weather.com.cn/g/    /// http://m.weather.com.cn/data/101190101.html    /// 20140531 涂聚文 Geovin Du    /// {"weatherinfo":{"city":"深圳","cityid":"101280601","temp":"32","WD":"西南风","WS":"4级","SD":"68%","WSE":"4","time":"16:40","isRadar":"1","Radar":"JC_RADAR_AZ9755_JB"}}    ///     public partial class WebForm1 : System.Web.UI.Page    {        string json_data = string.Empty;        string url = string.Empty;        //WeatherInfo we = new WeatherInfo();        ///         /// http://social.msdn.microsoft.com/Forums/en-US/4392c97a-3c6e-45b9-99c9-12a979c64910/c-20-jsonnet        ///         ///         ///         protected void Page_Load(object sender, EventArgs e)        {            try            {                url = "http://www.weather.com.cn/data/sk/101280601.html";                WebClient wc = new WebClient();                wc.Encoding = System.Text.Encoding.UTF8;//定义对象语言                json_data = wc.DownloadString(url);                //JsonConvert.DeserializeObject
(url); JsonSerializerSettings settings = new JsonSerializerSettings(); WeatherInfo we = JsonConvert.DeserializeObject
(json_data, new WeatherInfoConverter());// JsonConvert.DeserializeObject(json_data, Type.GetType, we); Response.Write("城市:"+we.city); Response.Write("城市代码:" + we.cityid); Response.Write("温度:" + we.temp); Response.Write("发布时间:" + we.time); //Response.Write(we.TheRest[""].ToString()); Response.Write("发风:" + we.WD); Response.Write("湿度:" + we.SD); Dictionary
dict = we.TheRest; //Response.Write(we.TheRest["WD"].ToString()); //for (int i = 0; i < dict.Count; i++) //{ // Response.Write(dict.Keys.ToString()); // Response.Write(dict.Values.ToString()); //} foreach (KeyValuePair
kvp in dict) { //outputBlock.Text += String.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value) + "\n"; string s = string.Format("键是:{0} 值是:{1}", kvp.Key.ToString(), (!object.Equals(kvp.Value, null) ? kvp.Value.ToString(): "")); Response.Write(s); } //Hashtable dict = new Hashtable(); //foreach (DictionaryEntry i in dict) //{ // string s = string.Format("键是:{0} 值是:{1}", i.Key.ToString(), i.Value.ToString()); // Response.Write(s); //} } catch (JsonReaderException tu) { Response.Write(tu.Message.ToString()); } } //.net 4.0 //private static T _download_serialized_json_data
(string url) where T : new() //{ // using (WebClient w = new WebClient()) // { // string json_data = string.Empty; // // attempt to download JSON data as a string // try // { // json_data = w.DownloadString(url); // } // catch (Exception) { } // // if string with JSON data is not empty, deserialize it to class and return its instance // return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject
(json_data) : new T(); // } //} }
(json_data); //var ser = new JavaScriptSerializer(); //we = _download_serialized_json_data

 

转载地址:http://gzdja.baihongyu.com/

你可能感兴趣的文章
谈面试中的亮点
查看>>
二叉树的简单实现即递归遍历
查看>>
Spring 实现自定义 bean 的扩展
查看>>
出现身份验证错误。要求的函数不受支持
查看>>
基于OpenCL的深度学习工具:AMD MLP及其使用详解
查看>>
vue+vux页面滚动定位(支持上下滑动)
查看>>
bzoj 2037: [Sdoi2008]Sue的小球——dp
查看>>
服务器数据库密码忘记
查看>>
andoid下的股票行情开发
查看>>
一些基本的正则表达式的验证方法
查看>>
Convert、Parse、TryParse、(int) 区别
查看>>
【PHPStorm使用手册】php interpreter is not configured
查看>>
自己用的很少的html标签
查看>>
仍然不足够--读后感
查看>>
IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
查看>>
Oracle 手工清除回滚段的几种方法
查看>>
算法:通过逻辑运算实现两数相加
查看>>
git 使用详解(5)-- get log 查看提交历史【转】
查看>>
Google 最新的 Fuchsia OS【科技讯息摘要】
查看>>
再看知名应用背后的第三方开源项目【转】
查看>>