csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | /// <summary> /// 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 /// </summary> 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; /// <summary> /// 城市名称 /// </summary> public string city { get { return _city; } set { _city = value; } } /// <summary> /// 城市代码 /// </summary> public string cityid { get { return _cityid; } set { _cityid = value; } //get; //set; } /// <summary> /// 温度 /// </summary> public string temp { get { return _temp; } set { _temp = value; } } /// <summary> /// 风向 /// </summary> public string WD { get { return _WD; } set { _WD = value; } } /// <summary> /// 风级 /// </summary> public string WS { get { return _WS; } set { _WS = value; } } /// <summary> /// 湿度 /// </summary> public string SD { get { return _SD; } set { _SD = value; } } /// <summary> /// /// </summary> public string WSE { get { return _WSE; } set { _WSE = value; } } /// <summary> /// 发布时间 /// </summary> public string time { get { return _time; } set { _time = value; } } /// <summary> /// /// </summary> public string isRadar { get { return _isRadar; } set { _isRadar = value; } } /// <summary> /// /// </summary> public string Radar { get { return _Radar; } set { _Radar = value; } } private Dictionary< string , object > _theRest = new Dictionary< string , object >(); public Dictionary< string , object > TheRest { get { return _theRest; } } // public Dictionary<string, decimal> Rates { get; set; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /// <summary> /// http://www.weather.com.cn/data/sk/101280601.html /// 20140531 涂聚文 Geovin Du /// </summary> public class WeatherInfoConverter : CustomCreationConverter<WeatherInfo> { 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | /// <summary> /// 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"}} /// </summary> public partial class WebForm1 : System.Web.UI.Page { string json_data = string .Empty; string url = string .Empty; //WeatherInfo we = new WeatherInfo(); /// <summary> /// http://social.msdn.microsoft.com/Forums/en-US/4392c97a-3c6e-45b9-99c9-12a979c64910/c-20-jsonnet /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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<Table>(json_data); //var ser = new JavaScriptSerializer(); //we = _download_serialized_json_data<WeatherInfo>(url); JsonSerializerSettings settings = new JsonSerializerSettings(); WeatherInfo we = JsonConvert.DeserializeObject<WeatherInfo>(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< string , object > 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< string , object > 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<T>(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<T>(json_data) : new T(); // } //} } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!