前言:

  最近在做商家发布产品,调用京东sdk,发现问题很多,而且还是在我同事的帮助下完成的,摸索中,菜鸟还请高手门多多提携才好,入正题

首先是引用jd的sdk啦,京东sdk中发布商品需要调用一个

360buy.ware.get.attribute的接口和一个新增商品的接口

文档中提示接口调用如下(.net的)

360buy.ware.get.attribute调用示例(.net)

IJdClient client = new DefaultJdClient(url, appkey, appsecret);

CategoryAttributeSearchRequest req = new CategoryAttributeSearchRequest();

req.cid = "jingdong" ; req.iskeyprop = "jingdong" ; req.issaleprop = "jingdong" ; req.aid = "jingdong" ; req.fields = "jingdong" ;

CategoryAttributeSearchResponse response = client.Execute (req, token, DateTime.Now.ToLocalTime());

360buy.ware.add调用示例(.net)

 IJdClient client = new DefaultJdClient(url, appkey, appsecret);

 WareAddRequest req = new WareAddRequest();

 req.tradeno = "jingdong" ; req.warelocation = "jingdong" ; req.cid = "jingdong" ; req.shopcategory = "jingdong" ; req.title = "jingdong" ; req.upccode = "jingdong" ; req.optiontype = "jingdong" ; req.itemnum = "jingdong" ; req.stocknum = "jingdong" ; req.producter = "jingdong" ; req.wrap = "jingdong" ; req.length = "jingdong" ; req.wide = "jingdong" ; req.high = "jingdong" ; req.weight = "jingdong" ; req.costprice = "jingdong" ; req.marketprice = "jingdong" ; req.jdprice = "jingdong" ; req.notes = "jingdong" ; req.wareimage = "txt" ; req.packlisting = "jingdong" ; req.service = "jingdong" ; req.skuproperties = "jingdong" ; req.attributes = "jingdong" ; req.skuprices = "jingdong" ; req.skustocks = "jingdong" ; req.propertyalias = "jingdong" ; req.outerid = "jingdong" ; req.ispayfirst = "jingdong" ; req.iscanvat = "jingdong" ; req.isimported = "jingdong" ; req.ishealthproduct = "jingdong" ; req.isshelflife = "jingdong" ; req.shelflifedays = "jingdong" ; req.isserialno = "jingdong" ; req.isappliancescard = "jingdong" ; req.isspecialwet = "jingdong" ; req.warebigsmallmodel = "jingdong" ; req.warepacktype = "jingdong" ; req.inputpids = "jingdong" ; req.inputstrs = "jingdong" ; req.hascheckcode = "jingdong" ; req.adcontent = "jingdong" ; req.listtime = "jingdong" ;

 WareAddResponse response1 = client.Execute (req, token, DateTime.Now.ToLocalTime());

其中有req.Attributes,req.InputPids这两个的值需要调用attribute这个接口从中获得属性

url, appkey, appsecret这三个是密钥,就不说了,必填字段是req.cid,和req.fields
token, DateTime.Now.ToLocalTime()这三个可以不填

因为我要用到response1里面的skuid,而这个值只能通过response1的Attribute属性获得一个Attribute集合

List<Attribute>list=new <Attribute>();

而运行的时候会发现,即使参数都谢对了,也不会获得数据

原因就是:jdsdk面的Attribute属性和.net框架里面的Attribute重名了,

Attribute在.net里面是abstract,抽象类没有不能被创建对象

最后总结的解决办法是:用Http请求

第一步:首先要组织http请求的url地址

  jd有个api测试工具

  

当你填完参数,点击"提交测试"的时候会在提交参数一栏显示请求的url复制出来:

主要的搞定,下面我个方法,仅供参考

这是创建请求url的

public static string CreatUrl(string cid)
{
string url;
return url = "https://api.jd.com/routerjson?v=2.0&method=360buy.ware.get.attribute&app_key=" + appKey + "&access_token=" + accessToken + "&360buy_param_json={\"cid\":\"" + cid + "\",\"is_key_prop\":\"\",\"is_sale_prop\":\"\",\"aid\":\"\",\"fields\":\"cid,aid,name\"}&timestamp=" + DateTime.Now.ToString();
}

第二部 :用组织的url去请求,获取数据

  public static Dictionary<string, string> GetAttribute(string cid)
{
string url = CreatUrl(cid); Encoding dataEncode = Encoding.UTF8;
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
webReq.Method = "GET";
webReq.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string json = sr.ReadToEnd();
int count = JObject.Parse(json)["category_attribute_search_response"]["attributes"].Count();
Dictionary<string, string> dic = new Dictionary<string, string>();
var list = JObject.Parse(json)["category_attribute_search_response"]["attributes"];
for (int i = ; i < count; i++)
{
if (list[i]["name"].ToString() == "ISBN")
{
dic.Add("ISBN", list[i]["aid"].ToString());
}
if (list[i]["name"].ToString() == "版次")
{
dic.Add("版次", list[i]["aid"].ToString());
}
if (list[i]["name"].ToString() == "出版社")
{
dic.Add("出版社", list[i]["aid"].ToString());
}
} return dic;
}

其中有一步骤比较重要,

  string json = sr.ReadToEnd();得出来的是json字符串,需要解析
大体结构是这样的
 {
"category_attribute_search_response":
{"code":"","total":,"attributes":
[
{"aid":,"name":"主题词"},
{"aid":,"name":"读者对象"},
{"aid":,"name":"附件"},
{"aid":,"name":"中图法分类号"},
{"aid":,"name":"附件数量"},
{"aid":,"name":"朗读"},
{"aid":,"name":"编纂"},
{"aid":,"name":"注释"},
{"aid":,"name":"口述"},
{"aid":,"name":"整理"},
{"aid":,"name":"品牌"},
{"aid":,"name":"摄影"},
{"aid":,"name":"书写"},
{"aid":,"name":"出版社"},
{"aid":,"name":"包装 "},
{"aid":,"name":"版次"},
{"aid":,"name":"ISBN"},
{"aid":,"name":"印次"},
{"aid":,"name":"印刷时间"},
{"aid":,"name":"页数"},
{"aid":,"name":"字数"},
{"aid":,"name":"开本"},
{"aid":,"name":"套装数量"},
{"aid":,"name":"出版时间"},
{"aid":,"name":"正文语言"},
{"aid":,"name":"绘者"},
{"aid":,"name":"校对"},
{"aid":,"name":"编者"},
{"aid":,"name":"译者"},
{"aid":,"name":"外文名"},
{"aid":,"name":"著者"},
{"aid":,"name":"用纸"},
{"aid":,"name":"丛书名"},
{"aid":,"name":"品相"}
]
} }

最新文章

  1. log4j 配置INFO 和DEBUG 分布输出至两个文件
  2. 【荐】JavaScript编码风格
  3. Python内置的HTTP协议服务器SimpleHTTPServer
  4. poj2387 spfa求最短路
  5. db2 常用命令(二)
  6. SqlHelper 带详细中文注释
  7. spring boot 实践
  8. FingerChaser(3) 解题报告目录
  9. C#打印
  10. XML(二)之DTD——XML文件约束
  11. lua State加载部分库
  12. CodeForces-731A
  13. bzoj 4870: [Shoi2017]组合数问题
  14. 构建之法 chapter1 心得
  15. Hdoj 2187.悼念512汶川大地震遇难同胞——老人是真饿了 题解
  16. WPF:理解ContentControl——动态添加控件和查找控件
  17. jsp不解析el表达式,不识别jstl标签,找不到http://java.sun.com/jsp/jstl/core
  18. SaltStack 使用 Jinja2 模板
  19. Eclipse 工程使用相对路径导入Jar包设置
  20. 七、java数组

热门文章

  1. 前端后端分离,怎么解决SEO优化的问题呢?
  2. git安装使用详解
  3. 关于undefind
  4. 开始使用Material UI
  5. OpenGl中使用着色器的基本步骤及GLSL渲染简单示例
  6. 线性滤波器(linear filter)与非线性滤波器(non-linear filter)
  7. 编译wxWidgets —— windows、vc71、bcc32、MinGW与命令行
  8. 如何诊断rac环境sysdate 返回的时间错误
  9. Android在putString和getString使用方法
  10. ARIES算法简介