csharp: using using System.Web.Script.Serialization read json
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//先在项目中添加System.Web.Extensions引用.net 3.5
using System.Web.Script.Serialization;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Net; namespace JSONFromCS
{
public partial class WebForm3 : System.Web.UI.Page
{
/// <summary>
/// 将json数据反序列化为Dictionary
///http://www.weather.com.cn/static/html/legend.shtml 天气图示
/// </summary>
/// <param name="jsonData">json数据</param>
/// <returns></returns>
private Dictionary<string, object> JsonToDictionary(string jsonData)
{
//实例化JavaScriptSerializer类的新实例
JavaScriptSerializer jss = new JavaScriptSerializer();
try
{
//将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
return jss.Deserialize<Dictionary<string, object>>(jsonData);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{ string url = "http://www.weather.com.cn/data/cityinfo/101280601.html";
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.UTF8;//定义对象语言
string json = wc.DownloadString(url);
//string json = "{\"weatherinfo\":{\"city\":\"深圳\",\"cityid\":\"101280601\",\"temp\":\"32\",\"WD\":\"西南风\",\"WS\":\"4级\",\"SD\":\"68%\",\"WSE\":\"4\",\"time\":\"16:40\",\"isRadar\":\"1\",\"Radar\":\"JC_RADAR_AZ9755_JB\"}}";
StringBuilder strb = new StringBuilder();
Dictionary<string, object> dic = JsonToDictionary(json);//将Json数据转成dictionary格式
Dictionary<string, object> dataSet = (Dictionary<string, object>)dic["weatherinfo"];
//使用KeyValuePair遍历数据
foreach (KeyValuePair<string, object> item in dataSet)
{ strb.Append(item.Key + ":" + item.Value + "<br/>");//显示到界面 } Response.Write(strb.ToString());
}
}
}
csharp: using using System.Web.Script.Serialization read json的更多相关文章
- ASP.NET 使用 System.Web.Script.Serialization 解析 JSON (转)
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- 参考C# 使用 System.Web.Script.Serialization 解析 JSON
参考C# 使用 System.Web.Script.Serialization 解析 JSON 使用json需要引用到System.Web.Script.Serialization.习惯在解决方案右键 ...
- C# 使用 System.Web.Script.Serialization 解析 JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- 在.net2.0下使用System.Web.Script.Serialization;
最近,在弄json字符串转为对象.需要添加这个引用System.Web.Script.Serialization;因为版本必须是dotnet2.0的原因,发现很多解决方案不适合自己.故使用这种解决办法 ...
- System.Web.Script.Serialization引用找不到的问题
之前在项目中要使用JavascriptSerializer这个类,需要引入System.Web.Script.Serialization命名空间,但是在添加引用中找不到这个命名空间,后来才得知Syst ...
- using System.Web.Script.Serialization
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...
- System.Web.Script.Serialization的引用
解决方案: 找到C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 ==>System.Web.Extensions.d ...
- The type or namespace name 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
应该说是 .net4 的bug,没有所谓的 System.Web.Extensions.dll 库文件,需要将项目的 Target Framework修改为 3.5版本,才能加载System.Web. ...
- "System.Web" 中不存在类型或命名空间
System.Web”中不存在类型或命名空间名称script /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...
随机推荐
- 「Nosql」Redis小记-内存解析&内存消耗篇
*博客搬家:初版发布于 2017/08/12 18:32 原博客地址:https://my.oschina.net/sunqinwen/blog/1507171 Redis内存消耗分析 注:本文 ...
- js中元素、触点等各种距离的总结
每次碰到元素滚动呀.鼠标拖动呀之类的通过对比位置来触发事件的需求时,都要花很多时间来百度怎么取到自己想要的那个值,什么scrollTop.offset等等,今天就把这些东西总结一下,以后再使用的话,就 ...
- redis实现 msetex和 getdel命令
1.redis本身不提供 msetex命令(批量增加key并设置过期时间) class RedisExtend { private static final Logger logger = Logge ...
- Flutter Navigator operation requested with a context that does not include a Navigat
如下直接在 MaterialApp 中使用 Navigator 是会报 Navigator operation requested with a context that does not inclu ...
- python四则运算2.0
github项目地址: https://github.com/kongkalong/python PSP 预估耗时(分钟) Planning .Estimate 48*60 Development . ...
- 那些H5用到的技术(4)——弹幕
前言思路实现模式无限循环模式时间线模式停止显示弹幕 前言 以前玩卷轴射击游戏的时候,大量的BOSS子弹让我们无路可逃的时候,让我见识到了真正弹幕的威力,可自从A站B站火了之后,大量评论留言参与到了视频 ...
- CommandBehavior.CloseConnection有何作用
其用在ExecuteReader(c)中,返回对象前不能关闭数据库连接,须用CommandBehavior.CloseConnection: 这是一个关于实际知识点的问题,面试官考查的是应聘者数据库访 ...
- idea的插件zookeeper
平时用惯了ZooInspector,偶然知晓了idea的这个插件,试了一下感觉挺方便的 由于开发环境在内网,所以这里介绍内网方式(外网更简单). 1.下载插件 http://plugins.jetbr ...
- unity 解决与永久解决行尾不一致报警
虽然不影响使用,但一堆警告信息着实让人不爽,继续往下看. 用Notepad2的“查看->显示换行编码"查看发现通过Unity3D编辑器创建的脚本文件是以"LF"结尾 ...
- CSS的margin属性:详解margin属性
在网上看到的一篇文章,说的比较全面.原文地址:http://www.poluoluo.com/jzxy/201206/167007.html 你真的了解margin吗? 你知道margin有什么特性吗 ...