-2

When I make an API call I get the following response

{
"AdPlanId": 1,
"PlanName": "SWIFT Test plan",
"Description": "Descritpion for Test plan",
"NoVideosToPlay": 2,
"NoBannersToShow": 3,
"MinTimeBetweenAds": 5,
"MaxTimeBetwenAds": 15,
"MinDataConsumption": 25,
"MaxDataConsumption": 100,
"Created": "2017-01-24T16:35:13.042211",
"LastModified": "2017-01-24T16:35:13.042712",
"MarkedForDeletion": null,
"MarkedDeletedDate": null
}

which I then try to Deserialize using the C# code

var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
return new DBResult<T>(true, JsonConvert.DeserializeObject<T>(content));
}

I get a "Null object cannot be converted to a value type" exception in this line return new DBResult(true, JsonConvert.DeserializeObject(content)); Please what I'm I doing wrong?

user2721794
  • 401
  • 1
  • 4
  • 20
  • When debugging what do you have as a result of `JsonConvert.DeserializeObject(content)`? Probably null.. You should show what is the value of `content` and what is the type `T` – Gilad Green May 12 '17 at 11:35
  • you will need to put a NULL check before "return new DBResult(true, JsonConvert.DeserializeObject(content));" – Adil Ansari May 12 '17 at 11:44
  • @GiladGreen The value of "content" is this { "AdPlanId": 1, "PlanName": "SWIFT Test plan", "Description": "Descritpion for Test plan", "NoVideosToPlay": 2, "NoBannersToShow": 3, "MinTimeBetweenAds": 5, "MaxTimeBetwenAds": 15, "MinDataConsumption": 25, "MaxDataConsumption": 100, "Created": "2017-01-24T16:35:13.042211", "LastModified": "2017-01-24T16:35:13.042712", "MarkedForDeletion": "", "MarkedDeletedDate": "" } In that line. – user2721794 May 12 '17 at 11:50

1 Answers1

0

I think you should try to make a proxy class for the type of object you are receiving and write that instead of . It is mostly unable to convert it to type T.

Make a proxy class something like this and then replace the with

class ClassName{
  string AdPlanId {get; set;}
  string PlanName {get; set;}
  string Description {get; set;}
  string NoVideosToPlay {get; set;}
  string NoBannersToShow {get; set;}
  string MinTimeBetweenAds {get; set;}
  string MaxTimeBetwenAds {get; set;}
  string MinDataConsumption {get; set;}
  string MaxDataConsumption {get; set;}
  string Created {get; set;}
  string LastModified {get; set;}
  string MarkedForDeletion {get; set;}
  string MarkedDeletedDate {get; set;}
}

Then do something like this:

var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
 var content = await response.Content.ReadAsStringAsync();
 return new DBResult<ClassName>(true, JsonConvert.DeserializeObject<ClassName>(content));
}
Abhishek Maurya
  • 321
  • 1
  • 3
  • 13
  • This would have solved the problem I think but I have other calls that use that generic method to make calls and as such your solution will break things. – user2721794 May 12 '17 at 11:54
  • Then it will be good to convert it to something like this: JObject jObject = JObject.Parse(jsonString); Refer http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object – Abhishek Maurya May 12 '17 at 12:00