I am currently integrating with a third party API, they are accepting json string similar to the following format :
{
"test-specialism": null,
"salaryCollection": [
{
"id":
{
"jobtypeid": 1
},
"maxsalary": 564,
"minsalary": 123,
"salarycurrency": "GBP",
"salarytype": "A"
},
{
"id":
{
"jobtypeid": 2
},
"maxsalary": null,
"minsalary": null,
"salarycurrency": "GBP",
"salarytype": null
},
}],
}
And here is my object:
public class Salary {
public double Minimum { get; set; }
public double Maximum { get; set; }
public PaymentFrequency Frequency { get; set; }
public double Step { get; set; }
public int JobTypeId { get; set; }
public SalarySetting() { }
}
public class Alert {
public string Specialism {get;set;}
public Salary Permanent { get; set; }
public Salary Temporary { get; set; }
public Salary Contract { get; set; }
}
As you can see the object structure is very inconsistent with the JSON strucuture. What would be an ideal way to convert the object to that specified json string? I’ve tries JSONProperty to map the property, that doesn’t seem to work well in this case.
You can use System.Web.Script.Serialization.JavaScriptSerializer. Example :
And I will adding some instances to Lists :
Then I’m going to serialize it :