且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将c#中的genric列表转换为json

更新时间:2023-02-08 11:05:42

Microsoft的JavaScriptSerializer不是Json序列化程序!所以它会逃脱特殊的角色...

使用Json.NET - 好多了......

http://james.newtonking.com/json [ ^

I am try to covert a c# object to json but am getting json obj in this way

"{\"PkUserId\":1,\"CubicalName\":\"21547\"}"

System.Web.Script.Serialization.JavaScriptSerializer jSearializer =
                   new System.Web.Script.Serialization.JavaScriptSerializer();
            string list = jSearializer.Serialize(genricobj);



public class UserProfile
    {
        [DataMember]
        public int PkUserId { set; get; }

        [DataMember]
        public string CubicalName { get; set; }
}



the problem is the above json is not a correct json format.

List loginInfolist=new List();
if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
 foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                    
                    loginInfoDC = new UserProfile();
                    loginInfoDC.PkUserId = Convert.ToInt32(dr["pkUserId"]);
                    loginInfoDC.CubicalName = Convert.ToString(dr["CubicalName"]);
                    loginInfolist.add(loginInfoDC);
                } 

                }



i want to know how to serialise to json and how to differentiate it with list index
say list[0] json and the lit[1]json

meant all the serialised json of the list will be in same json obj how to differentiate

Microsoft's JavaScriptSerializer is not a Json serializer!!! So it will escape special chars...
Use Json.NET - much better...
http://james.newtonking.com/json[^]