0
我有一个对象与符号:如何序列化空对象C#来JSON
public class CompanyTeam
{
public string companyGuid { get; set; }
public string companyId { get; set; }
}
public class Team
{
public string teamGuid { get; set; }
public string teamName { get; set; }
public CompanyTeam company { get; set; }
}
团队对象有数据除了CompanyTeam。当我的序列化对象
json = new JavaScriptSerializer().Serialize(teamObject);
回报
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": null,
}
我尝试实例CompanyTeam对象,但以空数据返回对象:
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": {
"companyGuid" : null,
"companyId" : null
},
}
你怎么会得到这样的结果?有任何想法吗?
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": {},
}
我对你想要的1/2方式有点失落。什么代表你期望/想要的东西? – Trey
我不知道如何甚至可以用JavaScriptSerializer做到这一点,但如果您使用JSON.net(https://www.nuget.org/packages/Newtonsoft.Json/),则可以省略属性从序列化空值(http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm)。你也可以调整基本的任何东西,所以当序列化JSON时,它通常是首选项。 – ckuri
@Trey我需要的序列化结果有“公司”:{},它代表JSON中的空对象 – CMedina