0

I am processing an API response that I am getting back that looks like this. I am having an issue Deserializing part of it due to nesting levels having the same identifier and not able to name them the same in C# classes.

"Dishonours": { "Dishonours": [ { "AccountID": 5292142, "H1": "Dishonours", "H2": "Dishonours", "H3": "", "SH1": "", "Description": "{No Unique Description} (DIR DR RVSL,DIR DR RVSL FEE)", "Count": 2, "FrequencyDescription": "Infrequent Same Day", "FrequencyDuration": "", "FrequencyDurationDate": "24Oct", "FrequencyWeekday": "", "FrequencyAmount": 47.2, "FrequencyAmountRange": "(6-88)", "TotalAmount": 94.4, "TotalInAmount": 88.4, "TotalOutAmount": 6, "MonthlyAmount": 0, "GroupID": "0-1130", "Display": "{No Unique Description}", "FrequencyExactness": "Infrequent", "FrequencyPeriod": "Same Day", "ScoreEmployer": null, "ScoreDirCr": null, "ScoreWeekday": null, "ScoreFrequency": null, "ScoreAmount": null, "ScoreTotal": 0 } ] }, "High Risk Merchants": { "High Risk Merchants": [ { "AccountID": 5292142, "H1": "High Risk Merchants", "H2": "High Risk Merchants", "H3": "", "SH1": "", "Description": "{NO MATCH}", "Count": null, "FrequencyDescription": null, "FrequencyDuration": null, "FrequencyDurationDate": null, "FrequencyWeekday": null, "FrequencyAmount": null, "FrequencyAmountRange": null, "TotalAmount": null, "TotalInAmount": null, "TotalOutAmount": null, "MonthlyAmount": null, "GroupID": null, "Display": null, "FrequencyExactness": null, "FrequencyPeriod": null, "ScoreEmployer": null, "ScoreDirCr": null, "ScoreWeekday": null, "ScoreFrequency": null, "ScoreAmount": null, "ScoreTotal": null } ] }

I have declared my C# classes like this but I am still getting a null reference error because it wont pick up the nested tag.

This is how I have deserialized it to this point.

private RootObjectContent DeserializeReportContent(string reportAttachmentContent)
{
    RootObjectContent attachedReportContent = null;

    string reportContentString = null;

    //System.Windows.Forms.MessageBox.Show(rptJsonContent.Replace(@"\", ""));

    using (StringReader contentReader = new StringReader(reportAttachmentContent.Replace(@"\", "")))
    {
        reportContentString = contentReader.ReadToEnd().ToString();
    }

    System.Windows.Forms.MessageBox.Show(reportContentString);

    JavaScriptSerializer returnRptContent = new JavaScriptSerializer();
    var serialContentJsonStr = returnRptContent.Serialize(reportContentString);

    attachedReportContent = returnRptContent.Deserialize<RootObjectContent>(reportContentString.Replace(@"\", ""));

    return attachedReportContent;

}

I am restricted to using SSIS in Visual Studio 2010 (.NET 4.0) and as such cant use Newtonsoft version 10.0.0.0.

Is there anyway around this?

public class Dishonour
{
    [JsonProperty("AccountID")]
    public int? AccountID { get; set; }
    [JsonProperty("H1")]
    public string H1 { get; set; }
    [JsonProperty("H2")]
    public string H2 { get; set; }
    [JsonProperty("H3")]
    public string H3 { get; set; }
    [JsonProperty("SH1")]
    public string SH1 { get; set; }
    [JsonProperty("Description")]
    public string Description { get; set; }
    [JsonProperty("Count")]
    public int? Count { get; set; }
    [JsonProperty("FrequencyDescription")]
    public string FrequencyDescription { get; set; }
    [JsonProperty("FrequencyDuration")]
    public string FrequencyDuration { get; set; }
    [JsonProperty("FrequencyDurationDate")]
    public string FrequencyDurationDate { get; set; }
    [JsonProperty("FrequencyWeekday")]
    public string FrequencyWeekday { get; set; }
    [JsonProperty("FrequencyAmount")]
    public decimal? FrequencyAmount { get; set; }
    [JsonProperty("FrequencyAmountRange")]
    public string FrequencyAmountRange { get; set; }
    [JsonProperty("TotalAmount")]
    public decimal? TotalAmount { get; set; }
    [JsonProperty("TotalInAmount")]
    public decimal? TotalInAmount { get; set; }
    [JsonProperty("TotalOutAmount")]
    public decimal? TotalOutAmount { get; set; }
    [JsonProperty("MonthlyAmount")]
    public decimal? MonthlyAmount { get; set; }
    [JsonProperty("GroupID")]
    public string GroupID { get; set; }
    [JsonProperty("Display")]
    public string Display { get; set; }
    [JsonProperty("FrequencyExactness")]
    public string FrequencyExactness { get; set; }
    [JsonProperty("FrequencyPeriod")]
    public string FrequencyPeriod { get; set; }
    [JsonProperty("ScoreEmployer")]
    public int? ScoreEmployer { get; set; }
    [JsonProperty("ScoreDirCr")]
    public int? ScoreDirCr { get; set; }
    [JsonProperty("ScoreWeekday")]
    public int? ScoreWeekday { get; set; }
    [JsonProperty("ScoreFrequency")]
    public int? ScoreFrequency { get; set; }
    [JsonProperty("ScoreAmount")]
    public int? ScoreAmount { get; set; }
    [JsonProperty("ScoreTotal")]
    public int? ScoreTotal { get; set; }
}

public class Dishonours
{
    [JsonProperty("Dishonours")]
    public List<Dishonour> Dishonour { get; set; }
}

public class HighRiskMerchant
{
    [JsonProperty("AccountID")]
    public int? AccountID { get; set; }
    [JsonProperty("H1")]
    public string H1 { get; set; }
    [JsonProperty("H2")]
    public string H2 { get; set; }
    [JsonProperty("H3")]
    public string H3 { get; set; }
    [JsonProperty("SH1")]
    public string SH1 { get; set; }
    [JsonProperty("Description")]
    public string Description { get; set; }
    [JsonProperty("Count")]
    public int? Count { get; set; }
    [JsonProperty("FrequencyDescription")]
    public string FrequencyDescription { get; set; }
    [JsonProperty("FrequencyDuration")]
    public string FrequencyDuration { get; set; }
    [JsonProperty("FrequencyDurationDate")]
    public string FrequencyDurationDate { get; set; }
    [JsonProperty("FrequencyWeekday")]
    public string FrequencyWeekday { get; set; }
    [JsonProperty("FrequencyAmount")]
    public double? FrequencyAmount { get; set; }
    [JsonProperty("FrequencyAmountRange")]
    public string FrequencyAmountRange { get; set; }
    [JsonProperty("TotalAmount")]
    public double? TotalAmount { get; set; }
    [JsonProperty("TotalInAmount")]
    public double? TotalInAmount { get; set; }
    [JsonProperty("TotalOutAmount")]
    public double? TotalOutAmount { get; set; }
    [JsonProperty("MonthlyAmount")]
    public double? MonthlyAmount { get; set; }
    [JsonProperty("GroupID")]
    public string GroupID { get; set; }
    [JsonProperty("Display")]
    public string Display { get; set; }
    [JsonProperty("FrequencyExactness")]
    public string FrequencyExactness { get; set; }
    [JsonProperty("FrequencyPeriod")]
    public string FrequencyPeriod { get; set; }
    [JsonProperty("ScoreEmployer")]
    public int? ScoreEmployer { get; set; }
    [JsonProperty("ScoreDirCr")]
    public int? ScoreDirCr { get; set; }
    [JsonProperty("ScoreWeekday")]
    public string ScoreWeekday { get; set; }
    [JsonProperty("ScoreFrequency")]
    public string ScoreFrequency { get; set; }
    [JsonProperty("ScoreAmount")]
    public double? ScoreAmount { get; set; }
    [JsonProperty("ScoreTotal")]
    public int? ScoreTotal { get; set; }
}

public class HighRiskMerchants
{
    [JsonProperty("High Risk Merchants")]
    public List<HighRiskMerchants> HighRiskMerchant { get; set; }
}
Lucas Perrett
  • 423
  • 1
  • 6
  • 16
  • Not sure I entirely understand your problem. Do you really have duplicated property names within the same object as in [this question](https://stackoverflow.com/q/20714160/3744182) or are you talking about wanting to flatten the nested hierarchy `"{ Dishonours": {"Dishonours": [ ...] } }` ? – dbc Jun 01 '17 at 23:50
  • Yes that is correct. It is not the greatest data. – Lucas Perrett Jun 01 '17 at 23:52
  • Wait - which is correct? Duplicated names or nesting? – dbc Jun 01 '17 at 23:53
  • Nesting sorry. poorly worded. – Lucas Perrett Jun 01 '17 at 23:54

0 Answers0