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. PyQt5(1)——Qt Designer初探

    相关环境配置我们就不介绍了(网上有很多教程) Qt Designer 是一款十分强大的GUI工具,生成的文件为 .UI文件  可以通过命令转化为Py文件,简单来说我们可以通过拖拽方式生成界面,在通过简 ...

  2. json与xml简介

    <1>. json : JavaScript 对象表示法(JavaScript Object Notation) 语法: 数据在名称/值对中 数据由逗号分隔 花括号保存对象 方括号保存数组 ...

  3. c语言-求完全数的一个算法

    #include<iostream> #include<math.h> using namespace std; int main() { int n,i,j=0,k,a[10 ...

  4. 移动web之一像素问题

    一.为什么会有一像素问题 弄明白这个问题,首先要知道DPR了. DPR(device pixel ratio)pixel等于picture element.设备像素比,是默认缩放100%的情况下,即D ...

  5. SPOJ 1811 Longest Common Substring(求两个串的最长公共子串 || 或者n个串)

    http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 参考:https://www.cnblogs.com/autoint/p/10345276.html: ...

  6. Flutter Dialog 屏蔽返回键

    使用 WillPopScope + Future.value(false); 屏蔽返回键.代码如下: showDialog<Null>( context: context, // Buil ...

  7. es与hive整合

    在hive classpath中添加elasticsearch-hadoop.jar,以下方法任一种均可: 1.启动hiveserver2 前,在hive-site.xml文件中更改hive.aux. ...

  8. 接口自动化测试框架 :APIAutoTest框架

    前言 随着测试技术的发展,接口自动化测试逐渐成为各大公司投入产出比最高的测试技术.介入时间早,执行效率高,稳定性高的优点,让越来越多的公司引入接口自动化测试. 框架简介 APIAutoTest是处理A ...

  9. c# xml序列化和反序列化。也就是xml的解析和反解析。

    用习惯了newTownSoft.json 的json反序列化.碰到xml是真的不习惯. 每次json反序列化都是直接把json丢到bejson网站生成一个实体类,稍微修改修改一点点变量名.然后直接ne ...

  10. eclipspe导入hibernate的源代码

    看图: