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": "tbtest1062@taobaotest.com",
"buyer_email": "tbtest1062@taobaotest.com",
"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": "tbtest1061@taobaotest.com",
"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"
}
}
}
本解析数据例子,可以等效拓展到其他解析文件格式。
毕竟:技术不重要,思想最重要!
分享到:
相关推荐
标签中的“滚动”可能是指软件支持长JSON字符串的滚动浏览,而“截屏”可能意味着软件具备截图功能,允许用户快速捕获并保存格式化的JSON数据,方便分享或记录。 至于压缩包内的其他文件: - “去脚本之家看看.url...
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,因其易于人阅读和编写,同时也易于机器解析和生成,故成为数据传输的理想选择。 在你提供的压缩包中,包含三个JSON格式的文件,这通常意味着它们都...
4. **处理结果**:API调用后,会返回JSON格式的数据,SDK提供了解析JSON并封装成Java对象的工具,便于开发者处理和使用。 5. **错误处理**:在开发过程中,需要对可能出现的错误进行处理,如网络异常、API调用超时...
8. **数据存储**:抓取后的数据可能需要保存,可以学习如何将JSON数据写入文件或数据库。 9. **合规性问题**:了解并遵守网络爬虫的法律法规,尊重网站的robots.txt协议,不进行非法爬取。 综上所述,这个项目提供...
在这个Java爬虫中,它通过发送HTTP请求到淘宝服务器,然后解析返回的HTML或JSON数据,提取所需的商品详情,如商品名称、价格、评价等。 2. **HTTP和HTTPS协议**:爬虫需要理解和使用HTTP(超文本传输协议)和HTTPS...
Java模拟淘宝登录源码是一种常见的网络编程技术,主要用于自动化测试、数据抓取或者模拟用户行为。在这个场景中,我们使用的工具是HTTPClient库,这是一个强大的Java HTTP客户端应用编程接口,可以方便地处理HTTP...
针对每个平台,可能需要分析其特定的URL结构和数据格式,以正确地构造请求和解析响应。例如,对于某些平台,可能需要登录才能访问价格信息,这就需要用到HttpClient发送带有cookie的请求。 5. **价格降序**: 爬取...
3. **接入流程**:使用该SDK,首先需要在淘宝开放平台上注册并获取应用ID(AppKey)和应用密钥(AppSecret),然后根据SDK文档配置API的请求参数,通过HTTP请求调用API,最后解析返回的JSON或XML数据进行业务处理。...
如果商品详情数据是JSON格式,比如通过Ajax异步请求获取,我们可以分析网络请求,找出请求的URL和参数,然后直接发送对应的请求获取JSON数据。例如,使用requests的post()方法: ```python import json data = {'...
// 解析返回的JSON数据,提取地理位置信息 // ... } else { System.err.println("请求失败,状态码:" + response.getStatusLine().getStatusCode()); } } finally { response.close(); } } } ``` 在上述...
JSON.jar包是一个Java库,它提供了处理JSON对象的功能,使Java开发者能够方便地在Java程序中创建、解析和操作JSON数据。 在Java中,JSON.jar包通常包含了如org.json相关的类,例如JSONObject、JSONArray、JSON...
开发者需要根据具体需求,按照规定的HTTP请求方式(GET或POST)、URL格式、请求参数及数据格式进行调用。 4. **数据返回格式**:淘宝客API通常返回JSON格式的数据,这是一种轻量级的数据交换格式,易于人阅读和编写...
总的来说,“spark java echarts淘宝交易大数据”项目展示了如何利用Spark的强大计算能力,配合Java的编程便利性和Echarts的可视化优势,来处理和解析淘宝交易大数据,为企业提供有价值的洞察,优化决策过程。...
在Java中,可以使用第三方库如net.sf.json(现在已经被移到org.json包下)来解析JSON数据。这个库中的JSONObject类可以方便地将字符串转换为JSON对象,并且能够让我们用类似访问Java对象属性的方式来访问JSON对象中...
请求体可能需要编码成URL编码或JSON格式,取决于服务器的接口要求。 3. **处理登录凭证**:在实际的登录过程中,可能需要处理验证码、cookies等。验证码通常需要用户手动输入或通过OCR技术识别。而cookies是服务器...
本文将详细介绍如何通过Java调用淘宝API并解析JSON数据来实现这一功能。 首先,我们需要了解淘宝提供的手机归属地查询API。在本例中,API的URL是`...
在Java中,可以使用HttpURLConnection或OkHttp库发送HTTP请求,解析JSON数据,将数据转化为Java对象。对于本地数据存储,可以使用SharedPreferences保存用户偏好设置,SQLite数据库存储用户数据。 3. **网络请求与...