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的更多相关文章

  1. ASP.NET 使用 System.Web.Script.Serialization 解析 JSON (转)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  2. 参考C# 使用 System.Web.Script.Serialization 解析 JSON

    参考C# 使用 System.Web.Script.Serialization 解析 JSON 使用json需要引用到System.Web.Script.Serialization.习惯在解决方案右键 ...

  3. C# 使用 System.Web.Script.Serialization 解析 JSON

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  4. 在.net2.0下使用System.Web.Script.Serialization;

    最近,在弄json字符串转为对象.需要添加这个引用System.Web.Script.Serialization;因为版本必须是dotnet2.0的原因,发现很多解决方案不适合自己.故使用这种解决办法 ...

  5. System.Web.Script.Serialization引用找不到的问题

    之前在项目中要使用JavascriptSerializer这个类,需要引入System.Web.Script.Serialization命名空间,但是在添加引用中找不到这个命名空间,后来才得知Syst ...

  6. using System.Web.Script.Serialization

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

  7. System.Web.Script.Serialization的引用

    解决方案: 找到C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 ==>System.Web.Extensions.d ...

  8. 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. ...

  9. "System.Web" 中不存在类型或命名空间

    System.Web”中不存在类型或命名空间名称script  /找不到System.Web.Extensions.dll引用 添加引用就行了...“添加引用→.Net→System.Web.Ente ...

随机推荐

  1. Android 4.2真坑爹

    艹~~~,Android4.2真坑爹,4.1以前的方法都不能使用了. 操蛋呢...

  2. 通过Maven简单搭建SSM框架

    创建Maven就不用多说了,下面直接看Pom.xml里面的依赖吧 <properties> <!-- spring版本号 --> <spring.version>5 ...

  3. leetcode-788-Rotated Digits(使用vector替代if else的逐个判断)

    题目描述: X is a good number if after rotating each digit individually by 180 degrees, we get a valid nu ...

  4. 高阶篇:4.4)FMEA手册的疑问与不足(个人观点)

    本章目的:如题,述说FMEA手册第四版的疑问与不足. 1.前言 作者接触FMEA,并真正将其作为可靠性设计方法,也是在近几年的时候,所以不能说算是一个FMEA专家吧. 但作者也有一些自己的优势,就是自 ...

  5. thinkphp5.1的安装

    首先,下载composer.建议选择全屏安装.参考资料https://docs.phpcomposer.com/00-intro.html.安装到c盘.这个跟之前nodejs一样,都是装c盘.大概是全 ...

  6. my34_脚本冥等添加自动任务-mysql监控部署

    场景: 定义一套添加mysql监控的脚本,在mysql安装完毕后,一键执行添加监控   已有以下的等一系列命令可以读取mysql从库的延迟时间并推向influxdb,变化的部分为 -P 端口.-k k ...

  7. 在win7系统设置SQL Server2014 express为远程数据

    如何设置远程访问到SQLserver服务器(局域网内的设置) 1.首先,使用Windows+R键 输入services.msc 打开本地服务. *说明:①MSSQLSERVER是正式使用的SQL创建实 ...

  8. $bzoj1025-SCOI2009$游戏 群论 $dp$

    题面描述 \(windy​\)学会了一种游戏.对于\(1​\)到\(N​\)这\(N​\)个数字,都有唯一且不同的1到N的数字与之对应.最开始\(windy​\)把数字按顺序\(1,2,3,\cdot ...

  9. 在浏览器中输入URL后,执行的全部过程。(一次完整的http请求过程)

    整个流程如下: 域名解析 为了将消息从你的PC上传到服务器 上.需要用到1P协议.ARP协议和0SPF协议 发起TCP的3次握手 建立TCP连接后发起http请求 服务器响应htp请求 浏览器解析ht ...

  10. Robot Framework的安装、更新与卸载

    Robot Framework的安装.更新与卸载 一,安装RF前的准备 一般就三种执行环境 Python, Jython (JVM) 和 IronPython (.NET): 安装python: #T ...