C# JavaScriptSerializer不序列化第二级
我正在尝试使用JavaScriptSerializer,当我想序列化一个类的对象列表时,我得到了很好的结果,比如C# JavaScriptSerializer不序列化第二级,c#,json,serialization,C#,Json,Serialization,我正在尝试使用JavaScriptSerializer,当我想序列化一个类的对象列表时,我得到了很好的结果,比如 public class employee { public string id; //unique id of the employee public string displayName; public int displayFlag; public employee(string i, string d
public class employee
{
public string id; //unique id of the employee
public string displayName;
public int displayFlag;
public employee(string i, string dN, int dF)
{
id = i;
displayName = dN;
displayFlag = dF;
}
}
public class data2
{
public List<employee> detail;
}
(并且data2是data2类的对象)
然而,当我尝试对一些更复杂的事情做同样的事情时,比如
public class Ccanister
{
string identifier;
public Ccanister(string c)
{
identifier = c;
}
}
public class medicine
{
public string id; //unique id
public string displayName;
//and here an array of canisters
public List<Ccanister> canister;
}
public class dataMedicine
{
public List<medicine> detail; //Change this
}
我得到了错误的结果。
dataM有正确的结果(我调试了它),但是当jsonMedi得到结果时,容器列表“CANCON”总是空的。(在dataM中它不是空的)
我想知道我在这里做错了什么我想你需要一个默认的构造函数让序列化程序工作你能发布实际的输出吗?
容器的json是this:[{}]
还是this:[]
?我这样问是因为您将Ccanister
的identifier
字段设置为私有,这意味着它不会被序列化,但对象应该是私有的。[{}]
谢谢!这就是问题所在!
public class Ccanister
{
string identifier;
public Ccanister(string c)
{
identifier = c;
}
}
public class medicine
{
public string id; //unique id
public string displayName;
//and here an array of canisters
public List<Ccanister> canister;
}
public class dataMedicine
{
public List<medicine> detail; //Change this
}
string jsonMedi = json.Serialize(dataM);