怎样在后台把一个list创建成json字符串传个前台
展开全部
OjClass oc = new OjClass();
oc.StrSex = "男";
oc.StrName = "xiaoguan";
oc.StrTitle = "Manager";
List<OjClass> oclist = new List<OjClass>();
oclist.Add(oc);
oclist.Add(oc);
//使用JavaScriptSerializer来做序列化
System.Web.Script.Serialization.JavaScriptSerializer sj = new System.Web.Script.Serialization.JavaScriptSerializer();
//序列化List
string jsonArray = sj.Serialize(oclist);
Console.WriteLine(jsonArray);
string json = sj.Serialize(oc);
Console.WriteLine(json);
Console.ReadKey();
object j = sj.Deserialize(json, typeof(OjClass));
OjClass temp = j as OjClass;
Console.WriteLine(temp.StrTitle);
Console.Read();
oc.StrSex = "男";
oc.StrName = "xiaoguan";
oc.StrTitle = "Manager";
List<OjClass> oclist = new List<OjClass>();
oclist.Add(oc);
oclist.Add(oc);
//使用JavaScriptSerializer来做序列化
System.Web.Script.Serialization.JavaScriptSerializer sj = new System.Web.Script.Serialization.JavaScriptSerializer();
//序列化List
string jsonArray = sj.Serialize(oclist);
Console.WriteLine(jsonArray);
string json = sj.Serialize(oc);
Console.WriteLine(json);
Console.ReadKey();
object j = sj.Deserialize(json, typeof(OjClass));
OjClass temp = j as OjClass;
Console.WriteLine(temp.StrTitle);
Console.Read();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询