I'm consuming a rest service but when I de serialize the JSON string it throws this exception? What does this exception mean?
Class
public class Product
{
public string PROD
{
get { return prod; }
set { prod = value; }
}
//Department Number
[JsonProperty("DPID")]
public int DPID
{
get { return dpid; }
set { dpid = value; }
}
//Sub Department Number
[JsonProperty("SDID")]
public int SDID
{
get { return sdid; }
set { sdid = value; }
}
//Category Number
[JsonProperty("CGID")]
public int CGID
{
get { return cgid; }
set { cgid = value; }
}
//Sub Category Number
[JsonProperty("SCID")]
public int SCID
{
get { return scid; }
set { scid = value; }
}
//Product Description
[JsonProperty("PDSC")]
public string PDSC
{
get { return pdsc; }
set { pdsc = value; }
}
//Product Brand
[JsonProperty("PBRN")]
public string PBRN
{
get { return pbrn; }
set { pbrn = value; }
}
//Season Code
[JsonProperty("SESN")]
public string SESN
{
get { return sesn; }
set { sesn = value; }
}
//Issue Quantity
[JsonProperty("IQTY")]
public string IQTY
{
get { return iqty; }
set { iqty = value; }
}
//Currency Code
[JsonProperty("CURR")]
public string CURR
{
get { return curr; }
set { curr = value; }
}
//Selling Price
[JsonProperty("SELL")]
public decimal SELL
{
get { return sell; }
set { sell = value; }
}
//Product SKU Code
[JsonProperty("PSKU")]
public string PSKU
{
get { return psku; }
set { psku = value; }
}
//Product Size
[JsonProperty("PSZE")]
public string PSZE
{
get { return psze; }
set { psze = value; }
}
//Product Colour
[JsonProperty("PCOL")]
public string PCOL
{
get { return pcol; }
set { pcol = value; }
}
//Pre-pack Code
[JsonProperty("PPCD")]
public string PPCD
{
get { return ppcd; }
set { ppcd = value; }
}
//Image URL
public string IURL
{
get { return iurl; }
set { iurl = value; }
}
[JsonProperty("DPDS")]
public string DPDS
{
get { return dpds; }
set { dpds = value; }
}
}
Consumer
This is what I use to consume the rest service and then it sends me a JSON string which I de serialize into an object of type Observable Collection called products.
public ObservableCollection<Product> products = new ObservableCollection<Product>();
public async Task<ObservableCollection<Product>> GetProducts()
{
try
{
string uri = url + "/product;
_client.Timeout = TimeSpan.FromSeconds(300);
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, uri);
var response2 = await _client.SendAsync(message);
if (response2.IsSuccessStatusCode)
{
var content = await response2.Content.ReadAsStringAsync();
var prodlist = JsonConvert.DeserializeObject<ObservableCollection<Product>>(content);
products = prodlist;
return products;
}
else if (response2.StatusCode == HttpStatusCode.NotFound)
{
return products;
}
return products;
}
catch (JsonException ex)
{
throw ex;
}
}
JSON String
The URI returns a large string of this format when called.
{
"PROD": "5510B-BK ",
"DPID": 0,
"SDID": 0,
"CGID": 0,
"SCID": 0,
"SPID": 0,
"PDSC": "5510B BLACK BOAT SHOE ",
"PBRN": "Footwear Direct",
"SESN": "2018 ",
"IQTY": "Pair ",
"CURR": "ZAR",
"SELL": 0,
"PSKU": "5510B-BK ",
"PSZE": "12 ",
"PCOL": "BK ",
"PPCD": "A ",
"DPDS": "None "
}
Exception Message
Please note that this is what it says when pulling a single product but the same thing happen when pulling all object the position is all that changes then.
Error converting value "{"PROD":"5510B-BK ","DPID":0,"SDID":0,"CGID":0,"SCID":0,"SPID":0,"PDSC":"5510B BLACK BOAT SHOE ","PBRN":"Footwear Direct","SESN":"2018 ","IQTY":"Pair ","CURR":"ZAR","SELL":0.0000,"PSKU":"5510B-BK ","PSZE":"12 ","PCOL":"BK ","PPCD":"A ","DPDS":"None "}" to type 'WarehouseProMobile.Models.Product'. Path '', line 1, position 427.
Solved
Turns out my rest API serialized to JSON and when the object was sent over the network the web server also serialized my object this made the string useless. Fixed this by adjusting the API to only send objects.