LitJson JavaScriptSerializer

  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 
  8 using LitJson;
  9 using System.Data;
 10 using System.Collections;
 11 using System.Web.Script.Serialization;
 12 
 13 public class Person
 14 {
 15 
 16    public string Name { get; set; }
 17    public int Age { get; set; }
 18 }
 19 
 20 public partial class _Default : System.Web.UI.Page
 21 {
 22    protected void Page_Load(object sender, EventArgs e)
 23    {
 24       //Response.AddHeader("Content-Type", "application/json;");
 25       //Response.ContentType = "application/json";
 26       //Response.ContentEncoding = System.Text.Encoding.UTF8;
 27       #region LitJson练习
 28       JsonData jd = new JsonData();
 29       jd["sada"] = "dsad";
 30       jd["www"] = 123;
 31       JsonData jr = new JsonData();
 32       jr["m"] = jd;//可以赋值,也可以赋值jsondata
 33       jr["time"] = DateTime.Now.ToLocalTime().ToString();
 34       jr["qqq"] = 567;
 35       jr["asda"] = "dawd";
 36       string json = JsonMapper.ToJson(jr);//②步,jsondata不是json字符串,还要把jsondata序列化,
 37       Response.Write(json);
 38       //  {
 39       //    "m": {
 40       //        "sada": "dsad",
 41       //        "www": 123
 42       //             },
 43       //    "time": "2015-04-19 14:38:41",
 44       //    "qqq": 567,
 45       //    "asda": "dawd"
 46       // }
 47       #endregion
 48       #region ArrayList,List<>--->[]随意是数字,字符串
 49       //ArrayList arr1 = new ArrayList();
 50       //arr1.Add("dasda");
 51       //arr1.Add("mmm");
 52       //arr1.Add(99999);
 53       //string rdt = JsonMapper.ToJson(arr1);//["dasda","mmm",99999]
 54       //Response.Write(rdt);
 55 
 56       //List<int> list1 = new List<int>();
 57       //list1.Add(5);
 58       //list1.Add(8);
 59       //string rmt = JsonMapper.ToJson(list1);//[5,8]
 60       //Response.Write(rmt);
 61 
 62       //List<string> list = new List<string>();
 63       //list.Add("xiaoming");
 64       //list.Add("xiaohong");
 65       //string rat = JsonMapper.ToJson(list);//["xiaoming","xiaohong"]数组 
 66       //Response.Write(rat);
 67       #endregion
 68       #region 键值对-->{}字典Hashtable,Dictionary<>,序列化成json,唯一要求键要为字符串
 69 
 70       //Hashtable has = new Hashtable();
 71       //has.Add("1", "sada");
 72       //has.Add("5", 99);
 73       //string str = JsonMapper.ToJson(has);
 74       //Response.Write(str);
 75 
 76       //Dictionary<string, int> dic1 = new Dictionary<string, int>();
 77       //dic1.Add("1", 5);
 78       //dic1.Add("3", 9);
 79       //var json = JsonMapper.ToJson(dic1);
 80       //Response.Write(json);
 81 
 82       //Dictionary<string, string> dic = new Dictionary<string, string>();//
 83       //dic.Add("xiaohong", "28");
 84       //dic.Add("xiaolan", "22");
 85       //string json = JsonMapper.ToJson(dic);//{"xiaohong":"28","xiaolan":"22"}
 86       //Response.Write(json); 
 87       #endregion
 88       #region 无法将类型为“system.int32”的对象强制转换为类型“system.string”。
 89       //Hashtable has = new Hashtable();
 90       //has.Add(1, "sada");
 91       //has.Add(5, "sadjdi");
 92       //string str = JsonMapper.ToJson(has);//无法将类型为“System.Int32”的对象强制转换为类型“System.String”
 93       //Response.Write(str);
 94 
 95       //Dictionary<int, string> dic = new Dictionary<int, string>();//
 96       //dic.Add(1, "dawd");
 97       //dic.Add(9, "www");
 98       //string json = JsonMapper.ToJson(dic);
 99       //Response.Write(json);
100 
101       //Dictionary<int, int> dic1 = new Dictionary<int, int>();
102       //dic1.Add(1, 5);
103       //dic1.Add(3, 9);
104       //var json = JsonMapper.ToJson(dic1);
105       //Response.Write(json);//无法将类型为“System.Int32”的对象强制转换为类型“System.String” 
106       #endregion
107       #region JavaScriptSerializer,内置序列化的一种方式,同上,键为字符串
108       //JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
109       //var json = javascriptSerializer.Serialize(dic1);
110       //Response.Write(json); 
111       #endregion
112       #region 普通类-->{},JsonMapper.ToJson(),JavaScriptSerializer和序列化普通类
113       //Person p = new Person();
114       //p.Name = "like";
115       //p.Age = 24;
116       //string ret = JsonMapper.ToJson(p);//{"Name":"like","Age":24} 
117 
118       //Person p1 = new Person();
119       //p.Name = "like";
120       //p.Age = 24;
121       //JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
122       //var json = javascriptSerializer.Serialize(p1);
123       //Response.Write(json);
124       #endregion
125    }
126 }
全部代码

 对一个集合,要求不序列化完,对其中的几项序列化。这时内置序列化对象就不方便。这是用LitJson[“key”]=value;方便。

List<RechargeM> list = RecBL.GetTB(Gid);
foreach (var item in list)
{
JsonData jd = new JsonData();
jd["ID"] = Tools.GetString(item.Id);
jd["Num"] = item.cRech_Num;
jd["Money"] = item.mRech_Money;
jd["Date"] = Tools.GetString(item.dRech_Date);
row.Add(jd);
}
Jlist["RechargeLis"] = row;

原文地址:https://www.cnblogs.com/leee/p/4437230.html