京東sdk商家上架接口調用問題總結

前言:web

  最近在作商家發佈產品,調用京東sdk,發現問題不少,並且仍是在我同事的幫助下完成的,摸索中,菜鳥還請高手門多多提攜纔好,入正題json

首先是引用jd的sdk啦,京東sdk中發佈商品須要調用一個api

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

文檔中提示接口調用以下(.net的)app

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)

1 IJdClient client = new DefaultJdClient(url, appkey, appsecret);
2 
3 WareAddRequest req = new WareAddRequest();
4 
5 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" ;
6 
7 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集合ssh

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

而運行的時候會發現,即便參數都謝對了,也不會得到數據ide

緣由就是:jdsdk面的Attribute屬性和.net框架裏面的Attribute重名了,工具

Attribute在.net裏面是abstract,抽象類沒有不能被建立對象測試

最後總結的解決辦法是:用Http請求ui

第一步:首先要組織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去請求,獲取數據

 1  public static Dictionary<string, string> GetAttribute(string cid)
 2         {
 3             string url = CreatUrl(cid);
 4 
 5             Encoding dataEncode = Encoding.UTF8;
 6             HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
 7             webReq.Method = "GET";
 8             webReq.ContentType = "application/x-www-form-urlencoded";
 9             HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
10             StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
11             string json = sr.ReadToEnd();
12             int count = JObject.Parse(json)["category_attribute_search_response"]["attributes"].Count();
13             Dictionary<string, string> dic = new Dictionary<string, string>();
14             var list = JObject.Parse(json)["category_attribute_search_response"]["attributes"];
15             for (int i = 0; i < count; i++)
16             {
17                 if (list[i]["name"].ToString() == "ISBN")
18                 {
19                     dic.Add("ISBN", list[i]["aid"].ToString());
20                 }
21                 if (list[i]["name"].ToString() == "版次")
22                 {
23                     dic.Add("版次", list[i]["aid"].ToString());
24                 }
25                 if (list[i]["name"].ToString() == "出版社")
26                 {
27                     dic.Add("出版社", list[i]["aid"].ToString());
28                 }
29             }
30 
31             return dic;
32         }

其中有一步驟比較重要,

  string json = sr.ReadToEnd();得出來的是json字符串,須要解析
大致結構是這樣的
 1 {
 2     "category_attribute_search_response":
 3     {"code":"0","total":34,"attributes":
 4         [
 5         {"aid":10074378,"name":"主題詞"},
 6         {"aid":10074379,"name":"讀者對象"},
 7         {"aid":10074376,"name":"附件"},
 8         {"aid":10074377,"name":"中圖法分類號"},
 9         {"aid":10074380,"name":"附件數量"},
10         {"aid":10074370,"name":"朗讀"},
11         {"aid":10074371,"name":"編纂"},
12         {"aid":10074368,"name":"註釋"},
13         {"aid":10074369,"name":"口述"},
14         {"aid":10074374,"name":"整理"},
15         {"aid":10074375,"name":"品牌"},
16         {"aid":10074372,"name":"攝影"},
17         {"aid":10074373,"name":"書寫"},
18         {"aid":10074351,"name":"出版社"},
19         {"aid":10074350,"name":"包裝 "},
20         {"aid":10074349,"name":"版次"},
21         {"aid":10074348,"name":"ISBN"},
22         {"aid":10074358,"name":"印次"},
23         {"aid":10074359,"name":"印刷時間"},
24         {"aid":10074356,"name":"頁數"},
25         {"aid":10074357,"name":"字數"},
26         {"aid":10074354,"name":"開本"},
27         {"aid":10074355,"name":"套裝數量"},
28         {"aid":10074352,"name":"出版時間"},
29         {"aid":10074353,"name":"正文語言"},
30         {"aid":10074366,"name":"繪者"},
31         {"aid":10074367,"name":"校對"},
32         {"aid":10074364,"name":"編者"},
33         {"aid":10074365,"name":"譯者"},
34         {"aid":10074362,"name":"外文名"},
35         {"aid":10074363,"name":"著者"},
36         {"aid":10074360,"name":"用紙"},
37         {"aid":10074361,"name":"叢書名"},
38         {"aid":10091564,"name":"品相"}
39         ]
40     }
41 
42 
43 }
相關文章
相關標籤/搜索