城里的
乡下鱼

asp.net解析json例子

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.Script.Serialization;    //导入解析Json的类   
  8.   
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         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'}]}";  
  15.         JavaScriptSerializer serializer = new JavaScriptSerializer();  
  16.         Dictionary<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(JSON);  
  17.         object[] info = (object[])json["info"];  
  18.         Dictionary<string, object> val;  
  19.         Dictionary<int, string> Objs = null;  
  20.         Dictionary<int, string> fileOldName = null;  
  21.         for (int i = 0; i < info.Length; i++)  
  22.         {  
  23.   
  24.             val = (Dictionary<string, object>)info[i];  
  25.   
  26.             foreach (KeyValuePair<string, object> str in val)  
  27.             {  
  28.                 //保存信息到 session   
  29.                 if (Session["bigfile_info"] == null)  
  30.                 {  
  31.                     Objs = new Dictionary<int, string>();  
  32.                     Session["bigfile_info"] = Objs;  
  33.                 }  
  34.                   
  35.                 if (Session["file_name"] == null)  
  36.                 {  
  37.                     fileOldName = new Dictionary<int, string>();  
  38.                     Session["file_name"] = fileOldName;  
  39.                 }  
  40.   
  41.                 if (str.Key.Equals("title"))  
  42.                 {  
  43.                     fileOldName[i + 1] = str.Value.ToString();  
  44.                 }  
  45.   
  46.                 if (str.Key.Equals("url"))  
  47.                 {  
  48.                     Objs[i + 1] = str.Value.ToString();  
  49.                 }  
  50.             }  
  51.   
  52.         }  
  53.         Session["bigfile_info"] = Objs;  
  54.         Session["file_name"] = fileOldName;  
  55.   
  56.         if (Session["file_name"] != null)  
  57.         {  
  58.             Objs = (Dictionary<int, string>)Session["file_name"];  
  59.             foreach (int i in Objs.Keys)  
  60.             {  
  61.                 lblJson.Text += Objs[i];  
  62.             }  
  63.         }  
  64.   
  65.     }  
  66. }  
posted on 2016-01-11 08:55  城里的乡下鱼  阅读(228)  评论(0编辑  收藏  举报