csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform

时间:2022-09-01 16:10:13
 /// <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; }
}
/// <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;
}
}
  /// <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();
// }
//} }

http://james.newtonking.com/json

csharp:using Newtonsoft.Json.Net2.0 in .net 2.0 webform的更多相关文章

  1. C&num; 使用Newtonsoft&period;Json&period;dll 格式化显示Json串

    private string ConvertJsonString(string str) { //格式化json字符串 JsonSerializer serializer = new JsonSeri ...

  2. &lbrack;DNX&rsqb;解决dnu restore时找不到Newtonsoft&period;Json的问题

    在Mac上用最新版的dnx 1.0.0-beta5-11855进行dnu restore,出现下面的错误: System.IO.FileNotFoundException: Could not loa ...

  3. ASP&period;NET2&period;0 Newtonsoft&period;Json 操作类分享

    JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...

  4. 【&period;net】&OpenCurlyDoubleQuote;Newtonsoft&period;Json”已拥有为&OpenCurlyDoubleQuote;Microsoft&period;CSharp”定义的依赖项。

    #事故现场: “Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项. #事故原因: 安装的Newtonsoft.Json版本为11.0.2,版本过高,与Micro ...

  5. Newtonsoft&period;Json&comma; Version&equals;3&period;5&period;0&period;0&comma; Culture&equals;neutral&comma; PublicKeyToken&equals;b9a188c8922137c6

    未能加载文件或程序集“Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b9a188c8922137c6”或它的某一个 ...

  6. 未能加载文件或程序集&OpenCurlyDoubleQuote;Newtonsoft&period;Json&comma; Version&equals;4&period;5&period;0&period;0&comma; Culture&equals;neutral&comma; PublicKeyToken&equals;30ad4fe6b2a6aeed”或它的某一个依赖项 解决方法

    在webconfig中加入这段话就可以了 <runtime>    <assemblyBinding xmlns="urn:schemas-microsoft-com:as ...

  7. 未能加载文件或程序集&OpenCurlyDoubleQuote;Newtonsoft&period;Json&comma; Version&equals;4&period;0&period;0&period;0&comma; Culture&equals;neutral&comma; PublicKeyToken&equals;30a &lbrack;问题点数:40分,结帖人u010259408&rsqb;

    未能加载文件或程序集“Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=30a [问题点数:40分,结帖人u01025 ...

  8. (已解决) 未能加载文件或程序集&OpenCurlyDoubleQuote;Newtonsoft&period;Json&comma; Version&equals;4&period;0&period;0&period;0&comma; Culture&equals;neutral&comma;

    在项目web.config里面添加: <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4F ...

  9. 无法解决&OpenCurlyDoubleQuote;Newtonsoft&period;Json&comma; Version&equals;6&period;0&period;0&period;0&comma; Culture&equals;neutral&comma; PublicKeyToken&equals;30ad4fe6b2a6aeed”与&OpenCurlyDoubleQuote;Newtonsoft&period;Json&comma; Version&equals;4&period;5&period;0&period;0&comma; Culture&equals;neutral&comma; PublicKeyToken&equals;30ad4fe6b2a6aeed”之间的冲突。正在随意选择&OpenCurlyDoubleQuote;Newtonsoft&period;Jso

    今天的程序莫名报错:  无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed” ...

随机推荐

  1. (总结)隐藏PHP版本与PHP基本安全设置

    为了安全起见,最好还是将PHP版本隐藏,以避免一些因PHP版本漏洞而引起的攻击. 1.隐藏PHP版本就是隐藏 “X-Powered-By: PHP/5.2.13″ 这个信息. 方法很简单:编辑php. ...

  2. Sublime Text3 常用快捷键

    1. 更改变量名的几种方法 a.选中变量,ctrl+d 一个个选择 b.选中变量,alt+F3   2.查找打开过的文件:Ctrl+P,然后输入最近的文件名就可以即时预览到文件内容. 3.ctrl+r ...

  3. 部分A&plus;B&lowbar;1

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA.例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6. 现给定A.DA.B.DB,请编 ...

  4. android高仿微信拍照、多选、预览、删除&lpar;去除相片&rpar;相冊功能

    先声明授人与鱼不如授人与渔,仅仅能提供一个思路,当然须要源代码的同学能够私下有偿问我要源代码:QQ:508181017 工作了将近三年时间了,一直没正儿八经的研究系统自带的相冊和拍照,这回来个高仿微信 ...

  5. Uva140 Bandwidth 全排列&plus;生成测试法&plus;剪枝

    参考过仰望高端玩家的小清新的代码... 思路:1.按字典序对输入的字符串抽取字符,id[字母]=编号,id[编号]=字母,形成双射       2.邻接表用两个vector存储,存储相邻关系     ...

  6. python加密

    ""#line:4 __all__ =[]#line:6 class OO0O0O000O0O0O000 :#line:8 ""#line:9 def __in ...

  7. SpringBoot2 task scheduler 定时任务调度器四种方式

    github:https://github.com/chenyingjun/springboot2-task 使用@EnableScheduling方式 @Component @Configurabl ...

  8. spoj220

    题解: 后缀数组 把所有串连接起来 二分答案 代码: #include<cstdio> #include<cstring> #include<algorithm> ...

  9. &lbrack;Ubuntu&rsqb; 关于使用 root 账号登录

    (本文验证环境为 Ubuntu 14.04 和 Lubuntu 13.04) Ubuntu 维护者们认为实在没有必要使用 root 帐户,因为你想做的所有事情管理员都可以完成,管理员只需使用 sudo ...

  10. javascrpt 页面格式化页面

    下面这个页面,格式化javaScript <html> <head> <title>JS格式化工具 </title> <meta http-equ ...