zoukankan      html  css  js  c++  java
  • ASP.net解析JSON例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Script.Serialization;    //导入解析Json的类
    
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string JSON = "{'info':[{'title':'list1','url':'/upload/209/20120814163339890.png'},{'title':'离职申请表','url':'/upload/209/20120814163339968.xls'},{'title':'申请表','url':'/upload/209/20120814163340046.doc'},{'title':'室内','url':'/upload/209/20120814163340156.jpg'},{'title':'心电图','url':'/upload/209/20120814163340218.gif'},{'title':'新建 文本文档','url':'/upload/209/20120814163340281.txt'}]}";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(JSON);
            object[] info = (object[])json["info"];
            Dictionary<string, object> val;
            Dictionary<int, string> Objs = null;
            Dictionary<int, string> fileOldName = null;
            for (int i = 0; i < info.Length; i++)
            {
    
                val = (Dictionary<string, object>)info[i];
    
                foreach (KeyValuePair<string, object> str in val)
                {
                    //保存信息到 session
                    if (Session["bigfile_info"] == null)
                    {
                        Objs = new Dictionary<int, string>();
                        Session["bigfile_info"] = Objs;
                    }
                    
                    if (Session["file_name"] == null)
                    {
                        fileOldName = new Dictionary<int, string>();
                        Session["file_name"] = fileOldName;
                    }
    
                    if (str.Key.Equals("title"))
                    {
                        fileOldName[i + 1] = str.Value.ToString();
                    }
    
                    if (str.Key.Equals("url"))
                    {
                        Objs[i + 1] = str.Value.ToString();
                    }
                }
    
            }
            Session["bigfile_info"] = Objs;
            Session["file_name"] = fileOldName;
    
            if (Session["file_name"] != null)
            {
                Objs = (Dictionary<int, string>)Session["file_name"];
                foreach (int i in Objs.Keys)
                {
                    lblJson.Text += Objs[i];
                }
            }
    
        }
    }
    

      

  • 相关阅读:
    Java:面向对象的编程语言
    Java基本数据类型
    NotePad++安装及配置
    MarkDown的黄金搭档Typora编辑器
    rabbitmq常用命令【转载】
    rabbitmqweb管理端口http://localhost:15672/ 无法访问可能出现的问题
    又一个关于TP5的坑 模板页注释了的 {aaa} 依然是会被访问的
    关于layui的分页
    使用TP5容易遇到的坑
    TP5访问不了控制器
  • 原文地址:https://www.cnblogs.com/qufly/p/3470914.html
Copyright © 2011-2022 走看看