Nulls after deserialising from JSON

  • Thread starter Thread starter dwjenkins
  • Start date Start date
D

dwjenkins

Guest
Hi there,

Looking for some assistance. I've getting data from the SalesForce API and some records will have data in its Parent, some wont have data in their Parent record because they dont have parents or the data isnt there.

I'm managing to deserialise the data from the returned JSON using

RootObject returnedData = JsonConvert.DeserializeObject<RootObject>(data);

foreach(var rootObject in returnedData.records)
{
record = rootObject;
}


The classes are:

public class RootObject
{
public int totalSize { get; set; }
public bool done { get; set; }
public Record[] records { get; set; }
}

public class Record
{
public Attributes attributes { get; set; }

public string Name { get; set; }
[JsonProperty("Account_Number__c")]
public string Abta { get; set; }

public Parent parent { get; set; }
public Owner owner { get; set; }
}

public class Attributes
{
public string type { get; set; }
public string url { get; set; }
}

public class Parent
{
public Attributes attributes { get; set; }
[JsonProperty("Account_Number__c")]
public string Abta { get; set; }
public string Name { get; set; }
}

public class Owner
{
public Attributes attributes { get; set; }

[JsonProperty("KamName")]
public string Name { get; set; }
}


I'm then doing the following:

Record returnedRecord = GetRecordData(bearerToken, item);

string AgentName = returnedRecord.Name == null ? "UnknownAgentName" : returnedRecord.Name;
string AgentAbta = returnedRecord.Abta == null ? "UnknownAgentAbta" : returnedRecord.Abta;
string ParentName = returnedRecord.parent.Name == null ? "UnknownParentName" : returnedRecord.parent.Name;
string ParentAbta = returnedRecord.parent.Abta == null ? "UnknownParentAbta" : returnedRecord.parent.Abta;
string KamName = returnedRecord.owner == null ? "UnknownKAM" : returnedRecord.owner.Name;

Console.WriteLine("{0} {1} {2} {3} {4}", AgentName, AgentAbta, ParentName, ParentAbta, KamName);


In some cases AgentAbta will be null and I "UnknownAgetAbta" - works fantastic.

In the current state if ParentAbta is NULL, I get a NULL reference. If I remove .Name, it always equates to NULL and I always get UnknownParentAbta.

I think I maybe missing something quite obvious here and google isnt helping!

It C# 7.3.

Thanks for any assistance you can offer!

Continue reading...
 
Back
Top