【发布时间】: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