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; us
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
        /// </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());
        }
    }
}

目录
相关文章
|
4月前
|
JSON JavaScript 数据格式
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能。
jwt-auth插件实现了基于JWT(JSON Web Tokens)进行认证鉴权的功能。
139 1
|
22天前
|
XML JSON 前端开发
【Web前端揭秘】XML与JSON:数据界的双雄对决,你的选择将如何改写Web世界的未来?
【8月更文挑战第26天】本文深入探讨了XML和JSON这两种广泛使用的数据交换格式在Web前端开发中的应用。XML采用自定义标签描述数据结构,适用于复杂层次数据的表示,而JSON则以键值对形式呈现数据,更为轻量且易解析。通过对两种格式的示例代码、结构特点及应用场景的分析,本文旨在帮助读者更好地理解它们的差异,并根据实际需求选择最合适的数据交换格式。
39 1
|
28天前
|
JSON 前端开发 JavaScript
|
4月前
|
JSON 前端开发 JavaScript
前端使用lottie-web,使用AE导出的JSON动画贴心教程
前端使用lottie-web,使用AE导出的JSON动画贴心教程
430 2
|
17天前
|
Java Spring 容器
彻底改变你的编程人生!揭秘 Spring 框架依赖注入的神奇魔力,让你的代码瞬间焕然一新!
【8月更文挑战第31天】本文介绍 Spring 框架中的依赖注入(DI),一种降低代码耦合度的设计模式。通过 Spring 的 DI 容器,开发者可专注业务逻辑而非依赖管理。文中详细解释了 DI 的基本概念及其实现方式,如构造器注入、字段注入与 setter 方法注入,并提供示例说明如何在实际项目中应用这些技术。通过 Spring 的 @Configuration 和 @Bean 注解,可轻松定义与管理应用中的组件及其依赖关系,实现更简洁、易维护的代码结构。
20 0
|
24天前
|
JSON Java API
【Azure Developer】如何验证 Azure AD的JWT Token (JSON Web 令牌)?
【Azure Developer】如何验证 Azure AD的JWT Token (JSON Web 令牌)?
|
25天前
|
JSON 数据格式
【应用服务 App Service】在Azure Web App的部署文件中,是否可以限制某些文件无法被访问?(如json)
【应用服务 App Service】在Azure Web App的部署文件中,是否可以限制某些文件无法被访问?(如json)
|
4月前
|
XML JSON 前端开发
【Web 前端】XML和JSON的区别?
【4月更文挑战第22天】【Web 前端】XML和JSON的区别?
【Web 前端】XML和JSON的区别?
|
4月前
|
JSON 算法 数据安全/隐私保护
聊聊 JSON Web Token (JWT) 和 jwcrypto 的使用
本文介绍了 JSON Web Token (JWT) 和 Python 中的 `jwcrypto` 库。JWT 是一种用于安全传输信息的紧凑型令牌,常用于身份验证。它由 Header、Payload 和 Signature 三部分组成,具有紧凑性、自包含和安全性等特点。`jwcrypto` 库提供了 JWT 的生成、验证、加密、解密及签名功能。通过该库,可以使用 RSA 等算法创建和验证 JWT,同时管理密钥对。安装 `jwcrypto` 可用 `pip install jwcrypto`,并示例展示了如何生成签名 JWT 和密钥对。
聊聊 JSON Web Token (JWT) 和 jwcrypto 的使用
|
4月前
|
JSON NoSQL Redis
ruoyi-nbcio发送消息的SerializationException: Could not read JSON: Unexpected character (‘¬‘ (code 172))
ruoyi-nbcio发送消息的SerializationException: Could not read JSON: Unexpected character (‘¬‘ (code 172))
78 0

热门文章

最新文章