0

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.

Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
  • 1
    If the variable name `productFeedXml` is any indication of what you are trying to do, then my assumption is that you are trying to parse `XML` as `JSON`. Verify that the data being returned is actually well formed `JSON` – Nkosi Feb 11 '16 at 15:02
  • Could you post the value of the variable "productFeedXml" that you are trying to parse? – Hernan Guzman Feb 12 '16 at 04:27

2 Answers2

1

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

Regfor
  • 8,515
  • 1
  • 38
  • 51
  • Instead of using the json api url, i was using the xml one. That returned the xml data. Correcting that has made things work perfectly. – Pankaj Upadhyay Feb 12 '16 at 06:35
  • Can you tell me what's happening in following line of code :: `JObject jObject = (JObject)JsonConvert.DeserializeObject(productFeedXml);` `var jProductData = jObject["productInfoList"];` – Pankaj Upadhyay Feb 12 '16 at 07:31
1

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);

SO Post

You can then parse jsonText using JObject.Parse(jsonText)

Community
  • 1
  • 1
Aamir Masood
  • 321
  • 2
  • 9
  • right. I was using the xml api url that was the error. I have edited the url for json now. works fine. – Pankaj Upadhyay Feb 12 '16 at 06:34
  • Can you tell me what's happening in following line of code :: JObject jObject = (JObject)JsonConvert.DeserializeObject(productFeedXml); var jProductData = jObject["productInfoList"]; – Pankaj Upadhyay Feb 12 '16 at 09:07