I am getting error 'Cannot convert null to a value type' when trying to deserialize JSON response into class object
I have the following code in C#
var client2 = new RestClient("https://portal.greeneis.com/v4_6_release/apis/3.0/company/contacts/"+CU.BillingContactId);
var request2 = new RestRequest(Method.GET);
request2.RequestFormat = DataFormat.Json;
request2.AddHeader("cache-control", "no-cache");
request2.AddHeader("authorization", "Basic " + token);
IRestResponse response2 = client2.Execute(request2);
JavaScriptSerializer oJS2 = new JavaScriptSerializer();
var oRootObject2 = oJS2.Deserialize<ContactClass.Rootobject>(response2.Content);
With C# class as
public class ContactClass
{
public class Rootobject
{
public int id { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public Type type { get; set; }
public Company company { get; set; }
public Site site { get; set; }
public string addressLine1 { get; set; }
public string addressLine2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
public string country { get; set; }
public Relationship relationship { get; set; }
public object department { get; set; }
public bool inactiveFlag { get; set; }
public string defaultMergeContactId { get; set; }
public string securityIdentifier { get; set; }
public string managerContactId { get; set; }
public string assistantContactId { get; set; }
public string title { get; set; }
public string school { get; set; }
public string nickName { get; set; }
public bool marriedFlag { get; set; }
public bool childrenFlag { get; set; }
public string significantOther { get; set; }
public string portalPassword { get; set; }
public int portalSecurityLevel { get; set; }
public bool disablePortalLoginFlag { get; set; }
public bool unsubscribeFlag { get; set; }
public string gender { get; set; }
public string birthDay { get; set; }
public string anniversary { get; set; }
public string presence { get; set; }
public string mobileGuid { get; set; }
public string facebookUrl { get; set; }
public string twitterUrl { get; set; }
public string linkedInUrl { get; set; }
public bool defaultBillingFlag { get; set; }
public Communicationitem[] communicationItems { get; set; }
public _Info4 _info { get; set; }
public string customFields { get; set; }
}
public class Type
{
public int id { get; set; }
public string name { get; set; }
public _Info _info { get; set; }
}
public class _Info
{
public string type_href { get; set; }
}
public class Company
{
public int id { get; set; }
public string identifier { get; set; }
public string name { get; set; }
public _Info1 _info { get; set; }
}
public class _Info1
{
public string company_href { get; set; }
}
public class Site
{
public int id { get; set; }
public string name { get; set; }
public _Info2 _info { get; set; }
}
public class _Info2
{
public string site_href { get; set; }
}
public class Relationship
{
public int id { get; set; }
public string name { get; set; }
public _Info3 _info { get; set; }
}
public class _Info3
{
public string relationship_href { get; set; }
}
public class _Info4
{
public DateTime lastUpdated { get; set; }
public string updatedBy { get; set; }
public string communications_href { get; set; }
public string notes_href { get; set; }
public string tracks_href { get; set; }
public string portalSecurity_href { get; set; }
public string image_href { get; set; }
}
public class Communicationitem
{
public int id { get; set; }
public Type1 type { get; set; }
public string value { get; set; }
public string extension { get; set; }
public bool defaultFlag { get; set; }
public string communicationType { get; set; }
}
public class Type1
{
public int id { get; set; }
public string name { get; set; }
public object _info { get; set; }
}
}
var oRootObject2 = oJS2.Deserialize<ContactClass.Rootobject>(response2.Content);
line is throwing error that ' Cannot convert null to a value type.'
Lots of solutions online suggesting creating custom Class
with nullable types but this won't work for me. I have no control over the Json
Thank you for asking a question here.
I had a similar issue in past while working in a project, you can create settings for JavascriptSerializer
which ignores Null value's
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
So your full code can be
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
JavaScriptSerializer oJS2 = new JavaScriptSerializer();
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var oRootObject2 = JsonConvert.DeserializeObject<ContactClass.Rootobject>(response2.Content, settings);
That's it, you should not get null value error now.
If your JSON is dynamic as you have mentioned you have no control over JSON, you can deserialize JSON into C# dynamic object like below using using Json.NET
dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'John Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 30}");
string name = stuff.Name; // John Smith
string address = stuff.Address.City;// New York
Or Also using Newtonsoft.Json.Linq
:
dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
string name = stuff.Name;
string address = stuff.Address.City;
OR check this link https://qawithexperts.com/article/web-api/quick-tip-to-convert-json-into-class-object/26 to quickly convert JSON into class object.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly