1.解析JSON格式数据需要的JAR包:
commons-beanutils.jar
commons-collections-3.1.jar
commons-lang-2.0.jar
commons-logging.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
log4j-1.2.11.jar
morph-1.1.1.jar
2. 解析数据核心代码
Java2JSON.java
package com.syctest.JSON; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com.syctest.bean.Order; import com.syctest.bean.OrderDetail; public class Java2JSON { public static void main(String[] args) { String str=getFileCode("E:\\json.txt"); // System.out.println(str); Java2JSON jj=new Java2JSON(); jj.analyticJson(str); Order userOrder=jj.analyticJson(str); System.out.println(userOrder.getReceiver_city()+" "+userOrder.getBuyer_nick()+" "+userOrder.getReceiver_address()); } /** * 解析从淘宝返回的订单详情数据 * @param jsonString:淘宝返回的JSON格式数据 * @return:订单详情数据 */ public Order analyticJson(String jsonString){ //JSON格式数据解析对象 JSONObject jb = JSONObject.fromObject(jsonString); JSONObject ja = jb.getJSONObject("trade_fullinfo_get_response").getJSONObject("trade"); //需返回的订单对象 Order order = new Order(); order.setBuyer_nick(ja.getString("buyer_nick")); order.setReceiver_address(ja.getString("receiver_address")); order.setReceiver_city(ja.getString("receiver_city")); order.setReceiver_name(ja.getString("receiver_name")); order.setReceiver_state(ja.getString("receiver_state")); //区域是否存在 if(ja.containsKey("receiver_district")){ order.setReceiver_district(ja.getString("receiver_district")); }else{ order.setReceiver_district(""); } //手机号是否存在 if(ja.containsKey("receiver_mobile")){ order.setReceiver_mobile(ja.getString("receiver_mobile")); }else{ order.setReceiver_mobile(""); } //电话是否存在 if(ja.containsKey("receiver_phone")){ order.setReceiver_phone(ja.getString("receiver_phone")); }else{ order.setReceiver_phone(""); } //订单明细列表 java.util.List<OrderDetail> goodsList = new java.util.LinkedList<OrderDetail>(); JSONArray orderDetailArr = ja.getJSONObject("orders").getJSONArray("order"); //循环添加订单明细 for(int j=0;j<orderDetailArr.size();j++){ OrderDetail od = new OrderDetail(); od.setNum(orderDetailArr.getJSONObject(j).getString("num")); // od.setOuter_iid(orderDetailArr.getJSONObject(j).getString("outer_iid")); od.setPayment(orderDetailArr.getJSONObject(j).getString("payment")); goodsList.add(od); } order.setGoodsList(goodsList); return order; } public static String getFileCode(String fileName) { String fileCode=""; try { BufferedInputStream in = new BufferedInputStream( new FileInputStream(fileName)); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); // System.out.println("Available bytes:" + in.available()); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); byte[] content = out.toByteArray(); fileCode = new String(content); // System.out.println("Readed bytes count:" + content.length); } catch (Exception e) { System.out.println("fileReadeCode() Exception :" + e.getMessage()); } return fileCode; } }
Order.java
package com.syctest.bean; import java.util.List; public class Order { String buyer_nick; List goodsList; String receiver_address; String receiver_city; String receiver_name; String receiver_state; String receiver_district; String receiver_mobile; String receiver_phone; public List getGoodsList() { return goodsList; } public void setGoodsList(List goodsList) { this.goodsList = goodsList; } public String getBuyer_nick() { return buyer_nick; } public void setBuyer_nick(String buyer_nick) { this.buyer_nick = buyer_nick; } public String getReceiver_address() { return receiver_address; } public void setReceiver_address(String receiver_address) { this.receiver_address = receiver_address; } public String getReceiver_city() { return receiver_city; } public void setReceiver_city(String receiver_city) { this.receiver_city = receiver_city; } public String getReceiver_name() { return receiver_name; } public void setReceiver_name(String receiver_name) { this.receiver_name = receiver_name; } public String getReceiver_state() { return receiver_state; } public void setReceiver_state(String receiver_state) { this.receiver_state = receiver_state; } public String getReceiver_district() { return receiver_district; } public void setReceiver_district(String receiver_district) { this.receiver_district = receiver_district; } public String getReceiver_mobile() { return receiver_mobile; } public void setReceiver_mobile(String receiver_mobile) { this.receiver_mobile = receiver_mobile; } public String getReceiver_phone() { return receiver_phone; } public void setReceiver_phone(String receiver_phone) { this.receiver_phone = receiver_phone; } }
OrderDetail.java
package com.syctest.bean; public class OrderDetail { String num; String outer_iid; String payment; public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getOuter_iid() { return outer_iid; } public void setOuter_iid(String outer_iid) { this.outer_iid = outer_iid; } public String getPayment() { return payment; } public void setPayment(String payment) { this.payment = payment; } }
淘宝返回json数据格式:
{ "trade_fullinfo_get_response": { "trade": { "adjust_fee": "0.00", "available_confirm_fee": "50.00", "buyer_alipay_no": "[email protected]", "buyer_email": "[email protected]", "buyer_message": "buyerMessage", "buyer_nick": "tbtest1062", "buyer_obtain_point_fee": 0, "buyer_rate": false, "cod_fee": "0.00", "created": "2010-05-11 10:32:51", "discount_fee": "0.00", "has_post_fee": true, "iid": "29e5b5d76cb3dc2b627cb3f94988592f", "modified": "2010-05-11 10:32:51", "num": 1, "orders": { "order": [{ "adjust_fee": "0.00", "buyer_rate": "false", "discount_fee": "0.00", "iid": "29e5b5d76cb3dc2b627cb3f94988592f", "num": 1, "oid": 22328007510301, "payment": "55.00", "pic_path": "http://img08.taobao.net/bao/uploaded/i8/T1jVXXXePbXXaoPB6a_091917.jpg", "price": "50.00", "refund_status": "NO_REFUND", "seller_rate": "false", "seller_type": "C", "sku_id": "5941486", "sku_properties_name": "颜色:天蓝色;尺码:M", "snapshot_url": "T1TVXXXn8UXXXfUYZ2.1269918494866_snap", "status": "TRADE_NO_CREATE_PAY", "title": "创建交易商品", "total_fee": "50.00" }] }, "payment": "55.00", "pic_path": "http://img08.taobao.net/bao/uploaded/i8/T1jVXXXePbXXaoPB6a_091917.jpg", "point_fee": 0, "post_fee": "5.00", "price": "50.00", "real_point_fee": 0, "received_payment": "0.00", "receiver_address": "下沙高教园区", "receiver_city": "北京市", "receiver_district": "东城区", "receiver_mobile": "12354252544", "receiver_name": "夏日香气", "receiver_phone": "0571-84110653", "receiver_state": "北京", "receiver_zip": "123456", "seller_alipay_no": "[email protected]", "seller_email": " ", "seller_name": "我在测试", "seller_nick": "tbtest1061", "seller_phone": "010-630239403-123", "seller_rate": false, "shipping_type": "express", "sid": "22328007510301", "snapshot_url": "T1TVXXXn8UXXXfUYZ2.1269918494866_snap", "status": "TRADE_NO_CREATE_PAY", "tid": 22328007510301, "title": "妙妙的经典小窝", "total_fee": "50.00", "type": "independent_shop_trade" } } }
本解析数据例子,可以等效拓展到其他解析文件格式。
毕竟:技术不重要,思想最重要!