I have an object with notation:
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; }
}
The Team object have data except CompanyTeam. When serialize my object
json = new JavaScriptSerializer().Serialize(teamObject);
return
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": null,
}
I try instance the CompanyTeam Object but return object with null data:
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": {
"companyGuid" : null,
"companyId" : null
},
}
How could you get this result? any ideas?
{
"teamGuid": "GUIDdata",
"teamName": "nameData",
"company": {},
}
JavaScriptSerializerand want to completely omit the properties ofCompanyTeamwhen they are null. Unfortunately there is no built-in functionality for this, instead you will need to use aJavaScriptConverteras shown in Can JavaScriptSerializer exclude properties with null/default values?.companyisnull, I know of no JSON serializer that would output what you expect; I would expect either to see"company":null, or not see anything at all. Could you just make it ... not benull?if(obj.company == null) ob.company = new CompanyTeam();should fix this, no?