阿里巴巴开放平台上获取产品分类和属性

       阿里巴巴开放平台有详细的API以及api tools在线测试工具,获取一些公开的数据非常方便。

       目前公司新的平台需要产品分类和产品公共属性,本来是打算让测试人员去整理的,后来发现工作量太大了,就想直接通过阿里巴巴平台上面把分类和属性数据导出来整理,开发人员花费了2天时间,导出了大概近1w条产品分类和18w条产品属性数据。

       产品分类是josn格式,json的数据结构如下

      

{
    "result": {
        "total": 56,
        "toReturn": [
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 122916001,
                "catsName": "宠物及园艺",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 1,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 122916002,
                "catsName": "汽车用品",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 2,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 58,
                "catsName": "照明工业",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 3,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 10166,
                "catsName": "女装",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 4,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 10165,
                "catsName": "男装",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 5,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 57,
                "catsName": "电子元器件",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 6,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 53,
                "catsName": "传媒、广电",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 7,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 70,
                "catsName": "安全、防护",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 8,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 68,
                "catsName": "包装",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 9,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 52,
                "catsName": "纸业",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 10,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 67,
                "catsName": "办公、文教",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 11,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 7,
                "catsName": "数码、电脑",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 12,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 5,
                "catsName": "电工电气",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 13,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 80,
                "catsName": "眼镜及配件",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 14,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 4,
                "catsName": "纺织、皮革",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 15,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 312,
                "catsName": "内衣",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": false,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 16,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 311,
                "catsName": "童装",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 17,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 54,
                "catsName": "服饰配件、饰品",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": false,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 18,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 1038378,
                "catsName": "鞋",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 19,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 1042954,
                "catsName": "箱包皮具",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 20,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            },
            {
                "supportMixWholesale": false,
                "tradeType": 1,
                "applySpu": false,
                "catsId": 3007,
                "catsName": "个人防护",
                "supportSKUPrice": false,
                "supportOnlineTrade": false,
                "batchPost": true,
                "childrenCats": [],
                "spuPriceExt": false,
                "leaf": false,
                "parentCats": [
                    {
                        "order": 56,
                        "parentCatsId": 0
                    }
                ],
                "applyRealPrice": false,
                "catsDescription": ""
            }
        ],
        "success": true
    }
}

 

       对应获取该json的代码:

   

     

 public static String loadJson(String categoryId) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL("https://gw.open.1688.com/openapi/param2/1/cn.alibaba.open/offerPostFeatures.get/1002774?categoryID="+categoryId);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

 

     json的解析代码:

 

 

   public static List<SurveyVO> jsonPar(String jsonStr)
            throws JSONException, SQLException {

        List<SurveyVO> listSurveyVO = new ArrayList<SurveyVO>();
        JSONObject jsonObject  = new JSONObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("result");
        JSONArray results = jsonObject.getJSONArray("toReturn");
 
        for (int i = 0; i < results.length(); i++) {
                SurveyVO surveyVO = new SurveyVO();
                List<String> listparentCategoryID = new ArrayList<String>();
                JSONObject result = results.getJSONObject(i);
                surveyVO.setCatsId(result.getString("catsId"));
                surveyVO.setCatsName(result.getString("catsName"));
                for (int count = 0; count < result.getJSONArray("parentCats").length(); count++) {
                    listparentCategoryID.add(result.getJSONArray("parentCats").getJSONObject(count)
                            .getString("parentCatsId"));

                }
                surveyVO.setListparentCategoryID(listparentCategoryID);
                listSurveyVO.add(surveyVO);
                
         }
 
        return listSurveyVO;

    }

 

        上述是对产品分类的操作,对产品属性只要遍历末级分类即可。

阿里巴巴开放平台 SDK 运行样例。 调用阿里巴巴开放平台API你是不是获取不到访问口令,看一下我的例子吧,原因是后台已经更改,而网站的说明没有更新。 IniFile ini = new IniFile(); Dictionary sl = new Dictionary(); sl = ini.GetSectionValues("Setting"); strAppKey = sl["appKey"].ToString(); strAccToken = sl["access_token"].ToString(); strAppSecret = sl["appSecret"].ToString(); string strTokeyTime = sl["datatime"].ToString(); string strRefresh_token = sl["refresh_token"].ToString(); //比较令牌保存时间,如果比现在早10个小时以上就更新 DateTime dt = Convert.ToDateTime(strTokeyTime); TimeSpan ts = System.DateTime.Now.Subtract(dt); if ((Int16.Parse(ts.Days.ToString()) >= 1) || (decimal.Parse(ts.Hours.ToString()) > 8)) { //超过有效期,重新获取Access_Token //利用Refresh_token获取access_token Dictionary ls = new Dictionary(); string urlStr = "https://gw.open.china.alibaba.com/openapi/http/1/system.oauth2/getToken/" + strAppKey; Dictionary dc = new Dictionary(); dc.Add("grant_type", "refresh_token"); dc.Add("need_refresh_token", "true"); dc.Add("client_id", strAppKey); dc.Add("client_secret", strAppSecret); dc.Add("redirect_uri", "http://localhost"); dc.Add("refresh_token", strRefresh_token); WebUtils wu = new WebUtils(); string tbxToken = wu.DoPost(urlStr, dc); Hashtable hs = (Hashtable)PluSoft.Utils.JSON.Decode(tbxToken); //保存令牌 ini.WriteValue("Setting", "access_token", hs["access_token"].ToString()); ini.WriteValue("Setting", "datatime", System.DateTime.Now.ToString()); strAccToken = hs["access_token"].ToString(); } 获取授权的CODE令牌,分两步。 一、获取CODE   将得到的CODE码复制到文本框中: 二、得到令牌,并存在本地   三、利用refresh_token获取access_token 四、两个例子: [Setting] appKey=输入你的App ID appSecret=输入你的App Secret Key refresh_token=f3863b17-dcf3-45f6-8787-481befb188c7 access_token=faf4a195-e87d-4bfd-afbd-dc804c264c9c datatime=2012-12-2 16:53:33 [tu] albumId=35150663 albumName=产品相册J imageCount=90 本Demo为接入阿里平台的“HelloWorld”,主要调用平台的免登录接口接入平台。Demo中对平台提供的SIP接 口返回值进行解析,其中解析签名部分适用于平台上提供的所有REST风格接口。 一、线上Demo运行效果看 本Demo已经部署到线上环境,您可以试订,订后在“我的软件”中将有此Demo入口,点击使用即可。 Demo订URL:http://mall.alisoft.com/apps/shopwindow/showAppDetailAction!view.jspa?appID=16857 二、Demo 结构介绍 1、本Demo采用Visual Studio2005开发的web网站项目。 2、Demo中主要文件介绍: Default.aspx,显示免登录是否成功页面,显示调用免登录系统级参数应用级参数。 Signature.cs ,签名参数生成工具类,其中code为软件注册后获得的CERT CODE。 三、本地部署 1.确认安装有Visual Studio或IIS,安装.net Framawork。 2.将项目导入Visual Studio或部署到IIS服务器,或者将项目拷贝到D盘,点击dotnethelloworld.sln打开项目。 3.配置CERT CODE Signature.cs中将code设置成自己注册软件的CERT CODE,因为签名会用到此安全编码。 四、运行 1.软件入口: 线上环境demo入口为:http://demo.aliapp.com/dotnethelloworld/Default.aspx 本地部署入口如为:http://localhost:1702/dotnethelloworld/Default.aspx 2.将软件入口录入到您注册软件的“软件测试入口”。 3.点击阿里软件集市平台中“我的软件”,找到自己部署软件,点击使用即可登录您本地的应用。 也可在开发者工作台中点击“免登录接口测试”,进入您刚部署的Demo应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值