using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; using LitJson;
using System.Data;
using System.Collections;
using System.Web.Script.Serialization; public class Person
{ public string Name { get; set; }
public int Age { get; set; }
} public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.AddHeader("Content-Type", "application/json;");
//Response.ContentType = "application/json";
//Response.ContentEncoding = System.Text.Encoding.UTF8;
#region LitJson练习
JsonData jd = new JsonData();
jd["sada"] = "dsad";
jd["www"] = ;
JsonData jr = new JsonData();
jr["m"] = jd;//可以赋值,也可以赋值jsondata
jr["time"] = DateTime.Now.ToLocalTime().ToString();
jr["qqq"] = ;
jr["asda"] = "dawd";
string json = JsonMapper.ToJson(jr);//②步,jsondata不是json字符串,还要把jsondata序列化,
Response.Write(json);
// {
// "m": {
// "sada": "dsad",
// "www": 123
// },
// "time": "2015-04-19 14:38:41",
// "qqq": 567,
// "asda": "dawd"
// }
#endregion
#region ArrayList,List<>--->[]随意是数字,字符串
//ArrayList arr1 = new ArrayList();
//arr1.Add("dasda");
//arr1.Add("mmm");
//arr1.Add(99999);
//string rdt = JsonMapper.ToJson(arr1);//["dasda","mmm",99999]
//Response.Write(rdt); //List<int> list1 = new List<int>();
//list1.Add(5);
//list1.Add(8);
//string rmt = JsonMapper.ToJson(list1);//[5,8]
//Response.Write(rmt); //List<string> list = new List<string>();
//list.Add("xiaoming");
//list.Add("xiaohong");
//string rat = JsonMapper.ToJson(list);//["xiaoming","xiaohong"]数组
//Response.Write(rat);
#endregion
#region 键值对-->{}字典Hashtable,Dictionary<>,序列化成json,唯一要求键要为字符串 //Hashtable has = new Hashtable();
//has.Add("1", "sada");
//has.Add("5", 99);
//string str = JsonMapper.ToJson(has);
//Response.Write(str); //Dictionary<string, int> dic1 = new Dictionary<string, int>();
//dic1.Add("1", 5);
//dic1.Add("3", 9);
//var json = JsonMapper.ToJson(dic1);
//Response.Write(json); //Dictionary<string, string> dic = new Dictionary<string, string>();//
//dic.Add("xiaohong", "28");
//dic.Add("xiaolan", "22");
//string json = JsonMapper.ToJson(dic);//{"xiaohong":"28","xiaolan":"22"}
//Response.Write(json);
#endregion
#region 无法将类型为“system.int32”的对象强制转换为类型“system.string”。
//Hashtable has = new Hashtable();
//has.Add(1, "sada");
//has.Add(5, "sadjdi");
//string str = JsonMapper.ToJson(has);//无法将类型为“System.Int32”的对象强制转换为类型“System.String”
//Response.Write(str); //Dictionary<int, string> dic = new Dictionary<int, string>();//
//dic.Add(1, "dawd");
//dic.Add(9, "www");
//string json = JsonMapper.ToJson(dic);
//Response.Write(json); //Dictionary<int, int> dic1 = new Dictionary<int, int>();
//dic1.Add(1, 5);
//dic1.Add(3, 9);
//var json = JsonMapper.ToJson(dic1);
//Response.Write(json);//无法将类型为“System.Int32”的对象强制转换为类型“System.String”
#endregion
#region JavaScriptSerializer,内置序列化的一种方式,同上,键为字符串
//JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
//var json = javascriptSerializer.Serialize(dic1);
//Response.Write(json);
#endregion
#region 普通类-->{},JsonMapper.ToJson(),JavaScriptSerializer和序列化普通类
//Person p = new Person();
//p.Name = "like";
//p.Age = 24;
//string ret = JsonMapper.ToJson(p);//{"Name":"like","Age":24} //Person p1 = new Person();
//p.Name = "like";
//p.Age = 24;
//JavaScriptSerializer javascriptSerializer = new JavaScriptSerializer();
//var json = javascriptSerializer.Serialize(p1);
//Response.Write(json);
#endregion
}
}
全部代码
对一个集合,要求不序列化完,对其中的几项序列化。这时内置序列化对象就不方便。这是用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;