ASP.net解析JSON

Posted 晴天天晴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<stringobject> json = (Dictionary<stringobject>)serializer.DeserializeObject(JSON);
        object[] info = (object[])json["info"];
        Dictionary<stringobject> val;
        Dictionary<intstring> Objs = null;
        Dictionary<intstring> fileOldName = null;
        for (int i = 0; i < info.Length; i++)
        {
 
            val = (Dictionary<stringobject>)info[i];
 
            foreach (KeyValuePair<stringobject> str in val)
            {
                //保存信息到 session
                if (Session["bigfile_info"] == null)
                {
                    Objs = new Dictionary<intstring>();
                    Session["bigfile_info"] = Objs;
                }
                 
                if (Session["file_name"] == null)
                {
                    fileOldName = new Dictionary<intstring>();
                    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<intstring>)Session["file_name"];
            foreach (int in Objs.Keys)
            {
                lblJson.Text += Objs[i];
            }
        }
 
    }
}

以上是关于ASP.net解析JSON的主要内容,如果未能解决你的问题,请参考以下文章

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

asp.net页面实用代码片段

ASP.Net webservices 中的 JSON 数组类型解析

ASP.net解析JSON

在 asp.net mvc 登录项目中解析 json 时出错

ASP.NET WebAPI和带有大型数据集的jQuery(json)