【问题标题】:Error in reading json API读取 json API 时出错
【发布时间】:2023-03-03 03:34:01
【问题描述】:

我正在尝试通过访问商店提供的 API 来获取产品列表。以下是我的代码

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

我收到以下错误::

Newtonsoft.Json.JsonReaderException:解析值时遇到意外字符:

【问题讨论】:

  • 如果变量名productFeedXml 表明您正在尝试做什么,那么我的假设是您尝试将XML 解析为JSON。验证返回的数据实际上格式正确JSON
  • 能否发布您尝试解析的变量“productFeedXml”的值?

标签: c# json asp.net-mvc api json.net


【解决方案1】:

您的源 JSON 字符串似乎无效或实际上不是 JSON 字符串。

请检查您发出的 HTTP 请求,服务器是否为您提供 JSON。根据您添加到 HTTP 请求的 Accept 标头,HTTP 服务可能会做出不同的响应。

为确保您向服务询问 JSON,您可以在请求中添加 Accept: application/JSON HTTP 标头。否则,服务器可能会自行决定并以 XML 响应您的请求。

如果您的服务器使用 JSON 响应,那么它可能会有所帮助:

var wc = new WebClient();
client.Headers.Set("Accept", "application/json");

您也可以检查您的回复的Content-Type,但是您需要使用另一种WebClient 方法,而不是DownloadString

【讨论】:

  • 我没有使用 json api url,而是使用了 xml。这返回了 xml 数据。纠正这一点使事情变得完美。
  • 你能告诉我下面这行代码发生了什么吗 :: JObject jObject = (JObject)JsonConvert.DeserializeObject(productFeedXml); var jProductData = jObject["productInfoList"];
【解决方案2】:

您似乎收到了 xml 响应。 如果添加 ACCEPT 标头没有帮助(如 refgor 所说),那么您可能需要先将 xml 序列化为 json,然后将其用作 JObject。这可能会有所帮助。

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);

SO Post

然后您可以使用 JObject.Parse(jsonText) 解析 jsonText

【讨论】:

  • 正确。我使用的是错误的 xml api url。我现在已经编辑了 json 的 url。工作正常。
  • 你能告诉我以下代码行发生了什么:: JObject jObject = (JObject)JsonConvert.DeserializeObject(productFeedXml); var jProductData = jObject["productInfoList"];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 2017-04-28
  • 1970-01-01
相关资源
最近更新 更多