csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform
2023-09-11 14:19:12 时间
/// 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
private Dictionary string, object _theRest = new Dictionary string, object public Dictionary string, object TheRest get { return _theRest; }
/// /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 objects 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 Im 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 objects 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; }
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」 原文:2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」 希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 命令行配置 1.新建控制台项目
///參考: http://james.newtonking.com/json/help/index.html# /// 塗聚文(Geovin Du) 20141228 /// 捷為工作室 /// /summary public partial class _Default : Sy
ASP.NET中XML转JSON的方法 原文:ASP.NET中XML转JSON的方法 许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理。要实现这一点,它们必须将XML格式转换为JSON格式。 XML转JSON代码
geovindu 读者是,读之者,者之读.一沙一世界! to be is to do举世皆清我独浊,众人皆醒我独醉.俺是农民工,程序员.
private Dictionary string, object _theRest = new Dictionary string, object public Dictionary string, object TheRest get { return _theRest; }
/// /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 objects 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 Im 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 objects 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; }
/// 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) 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(); // } }
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」 原文:2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」 希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 命令行配置 1.新建控制台项目
///參考: http://james.newtonking.com/json/help/index.html# /// 塗聚文(Geovin Du) 20141228 /// 捷為工作室 /// /summary public partial class _Default : Sy
ASP.NET中XML转JSON的方法 原文:ASP.NET中XML转JSON的方法 许多应用程序都将数据存储为XML的格式,而且会将数据以JSON的格式发送到客户端以做进一步处理。要实现这一点,它们必须将XML格式转换为JSON格式。 XML转JSON代码
geovindu 读者是,读之者,者之读.一沙一世界! to be is to do举世皆清我独浊,众人皆醒我独醉.俺是农民工,程序员.
相关文章
- Json.Net - Error getting value from 'ScopeId' on 'System.Net.IPAddress'
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到SUSE Linux Enterprise Server 12 SP2 64 位(GNOME 版本3.20.2)
- .NET开源Protobuf-net组件修炼手册
- C#.NET常见问题(FAQ)-如何把函数名作为参数传递给另一个函数
- C#.NET常见问题(FAQ)-使用SharpDevelop开发 如何在项目中添加类文件
- 这才是你需要的最基础的.Net基础面试题(通俗易懂,最基础的.Net)1
- asp.net中WinForm使用单例模式示例
- JQuery处理json与ajax返回JSON实例
- ASP.NET Core 6框架揭秘实例演示[31]:路由高阶用法
- ASP.NET Core静态文件中间件[1]: 搭建文件服务器
- .NET Core采用的全新配置系统[9]: 为什么针对XML的支持不够好?如何改进?
- VB.net:VB.net编程语言学习之基于VS软件连接SQL Server(利用ADO.NET操作数据库/添加新数据源/DataGridView数据表格控件)的简介、案例应用之详细攻略
- java.net.UnknownHostException: unknown host:XXXX异常解决办法
- ASP.NET Core 中间件 中间件(Middleware)和过滤器(Filter)的区别
- Dynamsoft Dynamic .NET TWAIN for net Crack
- net core HTTP Error 500.31 - Failed to load ASP.NET Core runtime HTTP Error 500.30