Question Background:
I am calling the Amazon Product Advertising API and receiving from their service a list of products in a JSON format.
The Issue:
Sadly the JSON response varies depending on the parameters supplied to the API. For example: searching for 'Sporting Goods' gives a slightly different response to 'Watches'.
The following gives a very simple example of the differences:
Sporting Goods JSON as received from the API:
{
"Item": {
"ID": "145",
"Title": "Football",
"ImageUrl": "http://amazon.com/football.jpg",
"Shipping": "UK",
"ListPrice": "7.99"
}
}
Watches JSON as received from the API:
{
"Item": {
"ID": "567",
"Title": "RolexWatch",
"ImageUrl": "http://amazon.com/RolexWatch.jpg",
"Shipping": "UK",
"ListPrice": "£7000.00",
"SalePrice": "£6500.00" <------------- extra item in Watches JSON
}
}
I am currently using NewtonSoft To deserialize the two responses into two distinct C# models i.e:
public class SportingGoods
{
public string ID {set;get;}
public string Title {set;get;}
public string ImagesUrl {set;get;}
public string Shipping{set;get;}
public string ListPrice{set;get;}
}
public class Watches
{
public string ID {set;get;}
public string Title {set;get;}
public string ImagesUrl {set;get;}
public string Shipping{set;get;}
public string ListPrice{set;get;}
public string SalePrice{set;get;}
}
I am finding there are other variations in the JSON response from the Amazon API. How can I handle this desearlization properly when the JSON data varies so much? I cant keep creating different models when I'm not 100% sure what the JSON will look like.