????????JSON????????????λ????google?????????д???????????Google??copy!????????????????json???????????????дdemo???????е????! ????????!
??????????????????! ???????????????????!?и????????ж?????ü??????????
????using System;
????using System.Collections.Generic;
????using System.Linq;
????using System.Web;
????using System.Runtime.Serialization.Json;
????using System.ServiceModel.Web;///?????????????????
????using System.IO;
????using System.Text;
????/// <summary>
????/// Summary description for JsonHelper
????/// </summary>
????public class JsonHelper
????{
????public JsonHelper()
????{
????//
????// TODO: Add constructor logic here
????//
????}
????/// <summary>
????/// ????????л? JSON ?????
????/// </summary>
????/// <typeparam name="T">????????</typeparam>
????/// <param name="obj">???????</param>
????/// <returns>JSON?????</returns>
????public static string GetJson<T>(T obj)
????{
????//??? ??????? System.ServiceModel.Web
????/**
????* ??????????????????System.Runtime.Serialization.Json; Json??????????
????* */
????DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
????using (MemoryStream ms = new MemoryStream())
????{
????json.WriteObject(ms?? obj);
????string szJson = Encoding.UTF8.GetString(ms.ToArray());
????return szJson;
????}
????}
????/// <summary>
????/// ??JSON?????????????
????/// </summary>
????/// <typeparam name="T">????????</typeparam>
????/// <param name="szJson">JSON?????</param>
????/// <returns>???????</returns>
????public static T ParseFormJson<T>(string szJson)
????{
????T obj = Activator.CreateInstance<T>();
????using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes(szJson)))
????{
????DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
????return (T)dcj.ReadObject(ms);
????}
????}
????}
?????????????
????public class TestData
????{
????public TestData()
????{
????}
????public int Id { get; set; }
????public string Name { get; set; }
????public string Sex { get; set; }
????}
??????????棺
????<%@ Page Language="C#" %>
????<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
????<script runat="server">
????protected void Page_Load(object sender?? EventArgs e)
????{
????string jsonStr = string.Empty;
????List<TestData> tds = new List<TestData>();
????//????????
????for (int i = 1; i < 4; i++)
????{
????tds.Add(new TestData() { Id = i?? Name = "jinho" + i?? Sex = "male" });
????}    //?????list????json?????
????jsonStr = JsonHelper.GetJson<List<TestData>>(tds);
????Response.Write(jsonStr);
????this.Page.ClientScript.RegisterStartupScript(this.GetType()?? "json"?? "getJson(" + jsonStr + ");"?? true);
????}
????</script>
????<script type="text/javascript">
????function getJson(jsonStr) {    //???eval????
????var json = eval(jsonStr);     //????????list????
????for (var i = 0; i < json.length; i++) {
????alert(json[i].Id + "Name:" + json[i].Name);
????}
????}
????</script>
????<html xmlns="http://www.w3.org/1999/xhtml">
????<head runat="server">
????<title></title>
????</head>
????<body>
????<form id="form1" runat="server">
????<div>
????</div>
????</form>
????</body>
????</html>
????????json???????????????λ????????????????????JsonHelper ????ж??e?