将c#中的genric列表转换为json [英] Converting genric list in c# to json
本文介绍了将c#中的genric列表转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将ac#object转换为json但是以这种方式获取json obj
{\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 {获得跨度>; set ; }
}
问题是以上json不是正确的json格式。
列表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想知道如何序列化到json以及如何区分它与列表索引
说列表[0] json和点亮[1] json
表示列表中所有序列化的json都将是在同一个json obj中如何区分
解决方案
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[^]
这篇关于将c#中的genric列表转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文