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.Runtime.Serialization.Json;

using System.IO;

using System.Text.RegularExpressions;

using System.Text;



namespace WebApplication1 {

    public partial class WebForm2 : System.Web.UI.Page {

        protected void Page_Load(object sender, EventArgs e) {

            if (!IsPostBack) {

                DataContractJsonSerializer JsonModel = new DataContractJsonSerializer(typeof(Person));

                DataContractJsonSerializer JsonModels = new DataContractJsonSerializer(typeof(List<Person>));

                MemoryStream MS1 = new MemoryStream();

                Person Pone = new Person() 

                { Age = 1, Name = "my", CreateTime = DateTime.Now.AddHours(1), BisFinish = false };

                Person Ptwo = new Person()

                { Age = 2, Name = "you", CreateTime = DateTime.Now.AddDays(1), BisFinish = true };

                JsonModel.WriteObject(MS1, Pone);

                string JsonOnePerson = Encoding.UTF8.GetString(MS1.ToArray());





                List<Person> Parr = new List<Person>();

                Parr.Add(Pone); Parr.Add(Ptwo);

                MemoryStream MS2 = new MemoryStream();

                JsonModels.WriteObject(MS2, Parr);

                string JsonPersons = Encoding.UTF8.GetString(MS2.ToArray());



                ConvJsonDate(JsonPersons);

                ConvJsonDate(JsonOnePerson);



                Response.Write(string.Format("<input id=\"Hidden1\" type=\"hidden\" value='{0}' />",

                    ConvJsonDate(JsonPersons)));

            }

        }



        public string ConvJsonDate(string str) {

            Regex Reg = new Regex(@"\\/Date\((\d+)\+(\d+)\)\\/");

            foreach (Match item in Reg.Matches(str)) {

                string strOld = item.Value.Replace("\\/Date(", "").Replace("+0800)\\/", ""); string strNew;

                DateTime DT = new DateTime(1970, 1, 1);

                strNew = DT.AddMilliseconds(double.Parse(strOld)).ToLocalTime().ToString("yy-MM-dd HH:mm:ss");

                str = str.Replace(item.Value, strNew);

            }

            return str;

        }

    }

    public class Person {

        public string Name { get; set; }

        public int Age { get; set; }

        public DateTime CreateTime { get; set; }

        public bool BisFinish { get; set; }

    }

}
View Code

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<script type="text/javascript" language="javascript" src="Scripts/json2.js"></script>

    <title></title>

</head>

<body>

    <form id="form1" method="get" runat="server">

    <div>

    </div>

    </form>

<script language="javascript" type="text/javascript">

    orderListJson = {};

    orderListJson = eval("(" + document.getElementById("Hidden1").value + ")");

    for (var i = 0; i < orderListJson.length; i++) {

        var obj = orderListJson[i];

    }  

</script>

</body>

</html>
View Code

 

你可能感兴趣的:(asp.net)