最近在做一个业务需求,需要通过速卖通提供的API,抓取速卖通订单数据到本地数据库。
速卖通官方提供了很多API及测试工具,抓取数据功能的实现基本上不存在问题,比较麻烦的是,ALi服务端传回来的订单列表是Json格式的。解析起来还是相当头疼的。
在此分享出本人的解析方法,个人感觉的比较愚笨的处理方式,希望各位大神给出更好的建议。
ALi提交的URL请求格式为:
其中参数内容为:
page=1 从第一页开始获取
pageSize=50 每页显示50条数据(ALi最大每页只能显示50条数据)
createDateStart=10%2F15%2F2013 抓取数据开始时间 10/15/2013
createDateEnd=10%2F16%2F2013 抓取数据结束时间 10/16/2013
orderStatus=WAIT_SELLER_SEND_GOODS 抓取订单状态
access_token 授权令牌
&_aop_signature 授权签名
通过URL获取回来的Json格式如下:
1 { 2 //订单总数 3 "totalItem": 2, 4 //订单列表 5 "orderList": [ 6 { 7 //子订单列表 8 "productList": [ 9 { 10 子订单ID 11 "childId": 60249667848471, 12 //备货时间 13 "goodsPrepareTime": 3, 14 //物流金额 15 "logisticsAmount": { 16 "amount": 4.53, 17 "cent": 453, 18 "currencyCode": "USD", 19 "centFactor": 100, 20 "currency": { 21 "defaultFractionDigits": 2, 22 "currencyCode": "USD", 23 "symbol": "$" 24 } 25 }, 26 //订单备注 27 "memo": "", 28 //卖家FirstName 29 "sellerSignerFirstName": "Lily", 30 //总产品金额 31 "totalProductAmount": { 32 "amount": 47.99, 33 "cent": 4799, 34 "currencyCode": "USD", 35 "centFactor": 100, 36 "currency": { 37 "defaultFractionDigits": 2, 38 "currencyCode": "USD", 39 "symbol": "$" 40 } 41 }, 42 //限时达 43 "freightCommitDay": "60", 44 //子订单是否能提交纠纷 45 "canSubmitIssue": false, 46 //产品单位 47 "productUnit": "piece", 48 //物流类型 49 "logisticsType": "CPAM", 50 //纠纷状态 51 "issueStatus": "NO_ISSUE", 52 //订单号 53 "orderId": 60249667848471, 54 //物流服务商 55 "logisticsServiceName": "China Post Air Mail", 56 //子订单状态 57 "sonOrderStatus": "PLACE_ORDER_SUCCESS", 58 //产品镜像链接 59 "productSnapUrl": "http://www.aliexpress.com/snapshot/275258937.html", 60 //假一赔三 61 "moneyBack3x": false, 62 //SKU编号 63 "skuCode": "MH0608W", 64 //产品ID 65 "productId": 1080625782, 66 //产品数量 67 "productCount": 1, 68 //妥投时间 69 "deliveryTime": "15-60", 70 //产品单位价格 71 "productUnitPrice": { 72 "amount": 47.99, 73 "cent": 4799, 74 "currencyCode": "USD", 75 "centFactor": 100, 76 "currency": { 77 "defaultFractionDigits": 2, 78 "currencyCode": "USD", 79 "symbol": "$" 80 } 81 }, 82 //卖家LastName 83 "sellerSignerLastName": "Zheng", 84 //产品图片链接 85 "productImgUrl": "http://img.alibaba.com/ordersnapshot//2013Q4_photo/137/437/275258937/1080625782_1.summ.jpg", 86 //状态显示 87 "showStatus": "PLACE_ORDER_SUCCESS", 88 //产品名称 89 "productName": "9500 Mini 3.5\" Capacitive Screen Android4.1 SC6820 Cheap Smartphone Dual SIM Quadband GSM WiFi Bluetooth Camera HK Free Shipping" 90 } 91 ], 92 //纠纷状态 93 "issueStatus": "NO_ISSUE", 94 //冻结状态 95 "frozenStatus": "NO_FROZEN", 96 //买家登录ID 97 "buyerLoginId": "ar1008081320", 98 //卖家全名 99 "sellerSignerFullname": "Lily Zheng",100 //101 "hasRequestLoan": false,102 //订单号103 "orderId": 60249667848471,104 //支付金额105 "payAmount": {106 "amount": 52.52,107 "cent": 5252,108 "currencyCode": "USD",109 "centFactor": 100,110 "currency": {111 "defaultFractionDigits": 2,112 "currencyCode": "USD",113 "symbol": "$"114 }115 },116 //订单创建117 "gmtCreate": "20131005163933000-0700",118 //订单状态119 "orderStatus": "PLACE_ORDER_SUCCESS",120 //买家全名121 "buyerSignerFullname": "graciela antosiuk",122 //超时剩余时间123 "timeoutLeftTime": 1267998346,124 //资金状态125 "fundStatus": "NOT_PAY",126 //订单类型127 "bizType": "AE_COMMON"128 },129 130 {131 "productList": [132 {133 "childId": 60244727279620,134 "goodsPrepareTime": 3,135 "logisticsAmount": {136 "amount": 4.53,137 "cent": 453,138 "currencyCode": "USD",139 "centFactor": 100,140 "currency": {141 "defaultFractionDigits": 2,142 "currencyCode": "USD",143 "symbol": "$"144 }145 },146 "memo": "",147 "sellerSignerFirstName": "Lily",148 "totalProductAmount": {149 "amount": 86.69,150 "cent": 8669,151 "currencyCode": "USD",152 "centFactor": 100,153 "currency": {154 "defaultFractionDigits": 2,155 "currencyCode": "USD",156 "symbol": "$"157 }158 },159 "freightCommitDay": "60",160 "canSubmitIssue": false,161 "productUnit": "piece",162 "logisticsType": "CPAM",163 "issueStatus": "NO_ISSUE",164 "orderId": 60244727279620,165 "logisticsServiceName": "China Post Air Mail",166 "sonOrderStatus": "PLACE_ORDER_SUCCESS",167 "productSnapUrl": "http://www.aliexpress.com/snapshot/275102127.html",168 "moneyBack3x": false,169 "skuCode": "CE0351W",170 "productId": 905202127,171 "productCount": 1,172 "deliveryTime": "15-60",173 "productUnitPrice": {174 "amount": 86.69,175 "cent": 8669,176 "currencyCode": "USD",177 "centFactor": 100,178 "currency": {179 "defaultFractionDigits": 2,180 "currencyCode": "USD",181 "symbol": "$"182 }183 },184 "sellerSignerLastName": "Zheng",185 "productImgUrl": "http://img.alibaba.com/ordersnapshot//2013Q4_photo/127/127/275102127/905202127_1.summ.jpg",186 "showStatus": "PLACE_ORDER_SUCCESS",187 "productName": "SANEI N78 Phone Call Tablet PC 7'' Capacitive Screen Crotex A9 MTK6515 Android 4.0 Dual Camera Bluetooth SIM Card Slot 3G HDMI"188 }189 ],190 "issueStatus": "NO_ISSUE",191 "frozenStatus": "NO_FROZEN",192 "buyerLoginId": "ng1014998014",193 "sellerSignerFullname": "Lily Zheng",194 "hasRequestLoan": false,195 "orderId": 60244727279620,196 "payAmount": {197 "amount": 91.22,198 "cent": 9122,199 "currencyCode": "USD",200 "centFactor": 100,201 "currency": {202 "defaultFractionDigits": 2,203 "currencyCode": "USD",204 "symbol": "$"205 }206 },207 "gmtCreate": "20131005075313000-0700",208 "orderStatus": "PLACE_ORDER_SUCCESS",209 "buyerSignerFullname": "rahman agbaje",210 "timeoutLeftTime": 1236418346,211 "fundStatus": "NOT_PAY",212 "bizType": "AE_COMMON"213 }214 ]215 }
解析的方式如下:
1 ///2 /// 获取速卖通订单列表Json 3 /// 4 public int GetAliExpressOrdersJson(string page,string pageSize,string accessToken) 5 { 6 string result = ""; 7 //总记录 8 int totalItem = -1; 9 //获取订单时间,前两天 10 DateTime startTime = DateTime.Now.AddDays(-2); 11 //转换获取订单时间 12 string dateStart = startTime.Month.ToString(CultureInfo.InvariantCulture) + "%2F" + 13 startTime.Day.ToString(CultureInfo.InvariantCulture) + "%2F" + 14 startTime.Year.ToString(CultureInfo.InvariantCulture); 15 //获取订单前一天 16 DateTime endTime = DateTime.Now.AddDays(-1); 17 //转换获取订单时间 18 string dateEnd = endTime.Month.ToString(CultureInfo.InvariantCulture) + "%2F" + 19 endTime.Day.ToString(CultureInfo.InvariantCulture) + "%2F" + 20 endTime.Year.ToString(CultureInfo.InvariantCulture); 21 22 string AliOrderStatus = ConfigurationManager.AppSettings["OrderStatus"]; 23 //拼接请求的URL 24 string urlPath = ConfigurationManager.AppSettings["FindOrderListQuery"] + "page=" + page + "&pageSize=" + pageSize + "&createDateStart=" + dateStart + "&createDateEnd=" + dateEnd + "&orderStatus=" + AliOrderStatus + "&access_token=" + accessToken; 25 Logger.WriteLog("获取从" + startTime.ToShortDateString() + "到" + endTime.ToShortDateString() + "之间订单的URL为:" + urlPath + "。"); 26 Console.WriteLine("获取从" + startTime.ToShortDateString() + "到" + endTime.ToShortDateString() + "之间订单的URL为:" + urlPath + "。"); 27 WebRequest req = WebRequest.Create(urlPath); 28 req.Method = "GET"; 29 WebResponse res = req.GetResponse(); 30 Stream receiveStream = res.GetResponseStream(); 31 Encoding encode = Encoding.GetEncoding("utf-8"); 32 StreamReader sr = new StreamReader(receiveStream, encode); 33 char[] readbuffer = new char[20480]; 34 int n = sr.Read(readbuffer, 0, 20480); 35 while (n > 0) 36 { 37 string str = new string(readbuffer, 0, n); 38 result += str; 39 n = sr.Read(readbuffer, 0, 20480); 40 } 41 JavaScriptSerializer JSS = new JavaScriptSerializer(); 42 object obj = JSS.DeserializeObject(result); 43 //获取Json对象,转换为键值对列表 44 Dictionarydatajson = (Dictionary )obj; 45 //创建订单列表及订单明细列表 46 var aliExpressOrderList = new List (); 47 var aliExpressOrderDetailList = new List (); 48 //解析键值对数据 49 #region 50 foreach (KeyValuePair item in datajson) 51 { 52 if (item.Key == "totalItem") 53 { 54 //订单总数 55 totalItem = Convert.ToInt32(item.Value.ToString()); 56 Logger.WriteLog("获取从" + startTime.ToShortDateString() + "到" + endTime.ToShortDateString() + "之间订单的总数为:" + totalItem + "。"); 57 Console.WriteLine("获取从" + startTime.ToShortDateString() + "到" + endTime.ToShortDateString() + "之间订单的总数为:" + totalItem + "。"); 58 } 59 else 60 { 61 //获取orderList对象 62 object[] orderList = (object[])item.Value; 63 64 foreach (Dictionary orders in orderList) 65 { 66 var aliExpressOrder = new AliExpressOrders(); 67 #region 68 69 //获取每一个Order 70 foreach (KeyValuePair order in orders) 71 { 72 if (order.Key == "productList") 73 { 74 object[] productList = (object[])order.Value; 75 //获取子订单列表 76 foreach (Dictionary products in productList) 77 { 78 var aliExpressOderDetail = new AliExpressOrderDetails(); 79 //获取每一个子订单 80 foreach (KeyValuePair product in products) 81 { 82 if (product.Key == "childId") 83 { 84 string childId = product.Value.ToString(); 85 aliExpressOderDetail.childId = product.Value.ToString(); 86 } 87 if (product.Key == "goodsPrepareTime") 88 { 89 string goodsPrepareTime = product.Value.ToString(); 90 aliExpressOderDetail.goodsPrepareTime = product.Value.ToString(); 91 } 92 if (product.Key == "logisticsAmount") 93 { 94 Dictionary logisticsAmountList = (Dictionary )product.Value; 95 foreach (var logisticsAmounts in logisticsAmountList) 96 { 97 if (logisticsAmounts.Key == "amount") 98 { 99 string amount = logisticsAmounts.Value.ToString();100 aliExpressOderDetail.logisticsAmount = Convert.ToDecimal(logisticsAmounts.Value.ToString());101 }102 }103 }104 if (product.Key == "memo")105 {106 string memo = product.Value.ToString();107 aliExpressOderDetail.memo = product.Value.ToString();108 }109 if (product.Key == "totalProductAmount")110 {111 Dictionary totalProductAmountList = (Dictionary )product.Value;112 foreach (var totalProductAmounts in totalProductAmountList)113 {114 if (totalProductAmounts.Key == "amount")115 {116 string amount = totalProductAmounts.Value.ToString();117 aliExpressOderDetail.totalProductAmount = Convert.ToDecimal(totalProductAmounts.Value.ToString());118 }119 }120 }121 if (product.Key == "freightCommitDay")122 {123 string freightCommitDay = product.Value.ToString();124 aliExpressOderDetail.freightCommitDay = product.Value.ToString();125 }126 if (product.Key == "canSubmitIssue")127 {128 string canSubmitIssue = product.Value.ToString();129 if (canSubmitIssue == "true")130 aliExpressOderDetail.canSubmitIssue = true;131 aliExpressOderDetail.canSubmitIssue = false;132 }133 if (product.Key == "productUnit")134 {135 string productUnit = product.Value.ToString();136 aliExpressOderDetail.productUnit = product.Value.ToString();137 }138 if (product.Key == "logisticsType")139 {140 string logisticsType = product.Value.ToString();141 aliExpressOderDetail.logisticsType = product.Value.ToString();142 }143 if (product.Key == "issueStatus")144 {145 string issueStatus = product.Value.ToString();146 aliExpressOderDetail.issueStatus = product.Value.ToString();147 }148 if (product.Key == "orderId")149 {150 string orderId = product.Value.ToString();151 aliExpressOderDetail.OrderID = product.Value.ToString();152 }153 if (product.Key == "logisticsServiceName")154 {155 string logisticsServiceName = product.Value.ToString();156 aliExpressOderDetail.logisticsServiceName = product.Value.ToString();157 }158 if (product.Key == "sonOrderStatus")159 {160 string sonOrderStatus = product.Value.ToString();161 aliExpressOderDetail.sonOrderStatus = product.Value.ToString();162 }163 if (product.Key == "productSnapUrl")164 {165 string productSnapUrl = product.Value.ToString();166 aliExpressOderDetail.productSnapUrl = product.Value.ToString();167 }168 if (product.Key == "moneyBack3x")169 {170 string moneyBack3x = product.Value.ToString();171 if (moneyBack3x == "true")172 aliExpressOderDetail.moneyBack3x = true;173 aliExpressOderDetail.moneyBack3x = false;174 }175 if (product.Key == "skuCode")176 {177 string skuCode = product.Value.ToString();178 aliExpressOderDetail.skuCode = product.Value.ToString();179 }180 if (product.Key == "productId")181 {182 string productId = product.Value.ToString();183 aliExpressOderDetail.productId = product.Value.ToString();184 }185 if (product.Key == "productCount")186 {187 string productCount = product.Value.ToString();188 aliExpressOderDetail.productCount = Convert.ToInt32(product.Value.ToString());189 }190 if (product.Key == "deliveryTime")191 {192 string deliveryTime = product.Value.ToString();193 aliExpressOderDetail.deliveryTime = product.Value.ToString();194 }195 if (product.Key == "productUnitPrice")196 {197 Dictionary productUnitPriceList = (Dictionary )product.Value;198 foreach (var productUnitPrices in productUnitPriceList)199 {200 if (productUnitPrices.Key == "amount")201 {202 string amount = productUnitPrices.Value.ToString();203 aliExpressOderDetail.productUnitPrice = Convert.ToDecimal(productUnitPrices.Value.ToString());204 }205 }206 }207 if (product.Key == "productImgUrl")208 {209 string productImgUrl = product.Value.ToString();210 aliExpressOderDetail.productImgUrl = product.Value.ToString();211 }212 if (product.Key == "showStatus")213 {214 string showStatus = product.Value.ToString();215 }216 if (product.Key == "productName")217 {218 string productName = product.Value.ToString();219 aliExpressOderDetail.productName = product.Value.ToString();220 }221 222 }223 aliExpressOrderDetailList.Add(aliExpressOderDetail);224 }225 }226 if (order.Key == "issueStatus")227 {228 string issueStatus = order.Value.ToString();229 aliExpressOrder.issueStatus = order.Value.ToString();230 }231 if (order.Key == "frozenStatus")232 {233 string frozenStatus = order.Value.ToString();234 aliExpressOrder.frozenStatus = order.Value.ToString();235 }236 if (order.Key == "buyerLoginId")237 {238 string buyerLoginId = order.Value.ToString();239 aliExpressOrder.buyerLoginId = order.Value.ToString();240 }241 if (order.Key == "sellerSignerFullname")242 {243 string sellerSignerFullname = order.Value.ToString();244 aliExpressOrder.sellerSignerFullname = order.Value.ToString();245 }246 if (order.Key == "orderId")247 {248 string orderId = order.Value.ToString();249 aliExpressOrder.orderId = order.Value.ToString();250 }251 if (order.Key == "payAmount")252 {253 Dictionary payAmountList = (Dictionary )order.Value;254 foreach (var payAmounts in payAmountList)255 {256 if (payAmounts.Key == "amount")257 {258 string amount = payAmounts.Value.ToString();259 aliExpressOrder.payAmount = Convert.ToDecimal(payAmounts.Value.ToString());260 }261 }262 }263 if (order.Key == "gmtCreate")264 {265 string gmtCreate = order.Value.ToString();266 aliExpressOrder.gmtCreate = order.Value.ToString();267 }268 if (order.Key == "orderStatus")269 {270 string orderStatus = order.Value.ToString();271 aliExpressOrder.orderStatus = order.Value.ToString();272 }273 if (order.Key == "buyerSignerFullname")274 {275 string buyerSignerFullname = order.Value.ToString();276 aliExpressOrder.buyerSignerFullname = order.Value.ToString();277 }278 if (order.Key == "timeoutLeftTime")279 {280 string timeoutLeftTime = order.Value.ToString();281 aliExpressOrder.timeoutLeftTime = order.Value.ToString();282 }283 if (order.Key == "fundStatus")284 {285 string fundStatus = order.Value.ToString();286 aliExpressOrder.fundStatus = order.Value.ToString();287 }288 if (order.Key == "bizType")289 {290 string bizType = order.Value.ToString();291 aliExpressOrder.bizType = order.Value.ToString();292 }293 }294 #endregion295 aliExpressOrderList.Add(aliExpressOrder);296 }297 }298 new AliExpressService().AddAliExpressOdersAndDetails(aliExpressOrderList, aliExpressOrderDetailList);299 }300 #endregion301 302 foreach (var orderse in aliExpressOrderList)303 {304 GetAliExpressOrdersDetailJson(orderse.orderId, accessToken);305 }306 307 return totalItem;308 }
个人感觉这种解析方式还是比较繁琐的。不知园子里的大牛们,是否能提供一种比较简便的解析Json的方式。