Reputation: 13574
I am trying to fetch a list of products by accessing the API prodvided by the store. Following is my code
public class CKProductAPI
{
public List<ProductListNames> ProductList(string url)
{
List<ProductListNames> objProducts = new List<ProductListNames>();
try
{
var wc = new WebClient();
wc.Headers.Add("Fk-Affiliate-Id", ConfigurationManager.AppSettings["FK-AFFID"]);
wc.Headers.Add("Fk-Affiliate-Token", ConfigurationManager.AppSettings["FK-TKN"]);
string productFeedXml = wc.DownloadString(url);
JObject jObject = (JObject)JsonConvert.DeserializeObject(productFeedXml);
var jProductData = jObject["productInfoList"];
foreach (var item in jProductData)
{
string strproductId, strtitle, strimageUrls, strmaximumRetailPrice, strsellingPrice, strcurrency, strproductBrand, strproductUrl, strinStock;
try { strproductId = item["productBaseInfo"]["productIdentifier"]["productId"].ToString(); }
catch { strproductId = ""; }
try { strtitle = item["productBaseInfo"]["productAttributes"]["title"].ToString(); }
catch { strtitle = ""; }
try { strimageUrls = item["productBaseInfo"]["productAttributes"]["imageUrls"].ToString(); }
catch { strimageUrls = ""; }
try { strmaximumRetailPrice = item["productBaseInfo"]["productAttributes"]["maximumRetailPrice"].ToString(); }
catch { strmaximumRetailPrice = ""; }
try { strsellingPrice = item["productBaseInfo"]["productAttributes"]["sellingPrice"].ToString(); }
catch { strsellingPrice = ""; }
try { strcurrency = item["productBaseInfo"]["productAttributes"]["currency"].ToString(); }
catch { strcurrency = ""; }
try { strproductBrand = item["productBaseInfo"]["productAttributes"]["productBrand"].ToString(); }
catch { strproductBrand = ""; }
try { strproductUrl = item["productBaseInfo"]["productAttributes"]["productUrl"].ToString(); }
catch { strproductUrl = ""; }
try { strinStock = item["productBaseInfo"]["productAttributes"]["inStock"].ToString(); }
catch { strinStock = ""; }
objProducts.Add(new ProductListNames
{
productId = strproductId,
title = strtitle,
imageUrls = strimageUrls,
maximumRetailPrice = strmaximumRetailPrice,
sellingPrice = strsellingPrice,
currency = strcurrency,
productBrand = strproductBrand,
productUrl = strproductUrl,
inStock = strinStock
});
}
}
catch (Exception)
{
throw;
}
return objProducts;
}
public class ProductListNames
{
public string productId { get; set; }
public string title { get; set; }
public string imageUrls { get; set; }
public string maximumRetailPrice { get; set; }
public string sellingPrice { get; set; }
public string currency { get; set; }
public string productUrl { get; set; }
public string productBrand { get; set; }
public string inStock { get; set; }
public string size { get; set; }
}
}
I am getting the following error ::
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
Upvotes: 0
Views: 123
Reputation: 321
It seems you are getting xml response. If adding ACCEPT header doesn't help (as refgor said) then you might need to serialize xml to json first then use it as JObject. This might help.
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
You can then parse jsonText using JObject.Parse(jsonText)
Upvotes: 1
Reputation: 8091
It seems to be your source JSON string is invalid or is not actually a JSON string.
Please check HTTP request you are making, whether server delivers you JSON. HTTP service might respond differently based on Accept header you are adding to your HTTP request.
To make sure you are asking service about JSON, you can add Accept: application/JSON
HTTP header to your request. Otherwise, server might decide by itself and respond with XML on your request.
If your server responds with JSON then it might help:
var wc = new WebClient();
client.Headers.Set("Accept", "application/json");
You can also check Content-Type
of your response but then you need to use another method of WebClient, not a DownloadString
Upvotes: 1