3

I have a C# classes and I need to parse JSON into it.

The class has a List<> from another class.

The class structure is like this.

public class OrderFund {
  public int OrderID { get; set; }
  public int BrokerID { get; set; }
  public string SettlementMethod { get; set; }
  public List<SettlementSap> SettlementsSap { get; set; }
}

public class SettlementSap {
  public string SapMonetaryAccountNo { get; set; }
  public string SapMonetaryAccountType { get; set; }
  public string SapMonetaryAccountOffice { get; set; }
}

My JSON is like this.

{
  "settlementMethod": "SAP",
  "BrokerID": 1,
  "OrderID": 1,
  "Settlements": [
    {
      "SapMonetaryAccountNo": "400245892464",
      "SapMonetaryAccountType": "CA",
      "SapMonetaryAccountOffice": "AR"
    }
  ]
}

I load my JSON file like this...

static OrderFund LoadJson(string file) {
  string dire = Directory.GetCurrentDirectory();
  using (StreamReader r = new StreamReader(dire + "\\" + file)) {
    string json = r.ReadToEnd();
    OrderFund items = JsonConvert.DeserializeObject<OrderFund>(json);
    return items;
  }
}

The data load fine into OrderFun Class but OrderFund.SettlementsSap is null. How can I load Settlements into SettlementsSap?

Manav
  • 553
  • 7
  • 18
Diego
  • 2,238
  • 4
  • 31
  • 68
  • change `public List SettlementsSap { get; set; }` to `public List Settlements { get; set; }` – er-sho Aug 13 '18 at 12:33
  • There is a Miss match between the Json and your class. Json say the properties is name Settlements when The class has a properies named SettlementsSap. You can modify your class to accept this by https://stackoverflow.com/questions/19792274/alternate-property-name-while-deserializing – Drag and Drop Aug 13 '18 at 12:34
  • 1
    Possible duplicate of [Alternate property name while deserializing](https://stackoverflow.com/questions/19792274/alternate-property-name-while-deserializing) – Drag and Drop Aug 13 '18 at 12:35

3 Answers3

4

That's because you have named the field SettlementsSap but your Json field is called Settlements...

You could rename the field in your class;

 public class OrderFund 
    {
        public int OrderID { get; set; }
        public int BrokerID { get; set; }
        public string SettlementMethod { get; set; }
        public List<SettlementSap> Settlements { get; set; }
    }

or add a [JsonProperty("Settlements")] attribute to the field like so;

public class OrderFund 
{
    public int OrderID { get; set; }
    public int BrokerID { get; set; }
    public string SettlementMethod { get; set; }
    [JsonProperty("Settlements")] 
    public List<SettlementSap> SettlementsSap { get; set; }
}
Milney
  • 6,253
  • 2
  • 19
  • 33
3

You just use a function of Visual Studio which convert your json into a model class

Goto: Edit -> Paste special -> Paste JSON as Class

The model class created by this feature will solve your problem

So, visiblely, you must rename SettlementsSap by Settlements

        public class OrderFund
        {
            public string settlementMethod { get; set; }
            public int BrokerID { get; set; }
            public int OrderID { get; set; }
            public Settlement[] Settlements { get; set; }
        }

        public class Settlement
        {
            public string SapMonetaryAccountNo { get; set; }
            public string SapMonetaryAccountType { get; set; }
            public string SapMonetaryAccountOffice { get; set; }
        }
Antoine V
  • 6,998
  • 2
  • 11
  • 34
0

The problem is with the naming. In the JSON, the name is Settlements. But in the class definition of OrderFund it is named as SettlementsSap

Shahzad
  • 1,677
  • 1
  • 12
  • 25