在泛型列表中绑定项目并进行序列化。

时间:2021-08-07 20:54:17

This is my code:

这是我的代码:

List<Test> list = new List<Test>();
for (int j = 0; j < dss.Tables[0].Rows.Count; j++)
{
    list.Add(new Test(dss.Tables[0].Rows[j]["PSet"].ToString(), 
                      Convert.ToInt32(dss.Tables[0].Rows[j]["Score"].ToString())));
}
StringBuilder data = new StringBuilder();
JavaScriptSerializer json = new JavaScriptSerializer();
json.Serialize(list, data);
hdlineData.Value = list.ToString();   

But it's not serializing my list, if i am not binding list correctly. Please suggest me how to bind list in Json format.

但是如果我没有正确的绑定列表,它不会序列化我的列表。请建议我如何用Json格式绑定列表。

1 个解决方案

#1


0  

You can use the [Serializable()] attribute on your custom class and then:

您可以在自定义类中使用[Serializable()]属性,然后:

JavaScriptSerializer serializer = new JavaScriptSerializer();
var Json = serializer.Serialize(myObject);

To ignore specific properties in the object you're serializing, simply place the [NonSerialized] attribure on them.

要忽略正在序列化的对象中的特定属性,只需将[NonSerialized] attribure放在它们上。

I Just Referred From HERE

我刚从这里提到。

#1


0  

You can use the [Serializable()] attribute on your custom class and then:

您可以在自定义类中使用[Serializable()]属性,然后:

JavaScriptSerializer serializer = new JavaScriptSerializer();
var Json = serializer.Serialize(myObject);

To ignore specific properties in the object you're serializing, simply place the [NonSerialized] attribure on them.

要忽略正在序列化的对象中的特定属性,只需将[NonSerialized] attribure放在它们上。

I Just Referred From HERE

我刚从这里提到。