FastJson可以把读取到的数据直接解析为对象即可,所以,你可需求知道json的数据格式,然后根据格式定制beanv即可,闲话少说,直接上代码
1. bean如下所示
package com.json.example; import java.io.Serializable; import java.util.List; public class Bean implements Serializable { private static final long serialVersionUID = -7722646761436254605L; private int beginPage; private int cacheSize; private int total; private List<?> data; public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public int getBeginPage() { return beginPage; } public void setBeginPage(int beginPage) { this.beginPage = beginPage; } public int getCacheSize() { return cacheSize; } public void setCacheSize(int cacheSize) { this.cacheSize = cacheSize; } public List<?> getData() { return data; } public void setData(List<?> data) { this.data = data; } @Override public String toString() { return "Bean [beginPage=" + beginPage + ",cacheSize=" + cacheSize + ",total=" + total + ",data=" + data + "]"; } } 2. 原json格式:
<pre class="plain" name="code">[beginPage=1,cacheSize=1,total=78,data=[{"address":"","area":"","asset_classes":"房产,土地使用权,设备,在建工程","assignee_requirement":"","bid_actual_end_date":"","bid_actual_start_date":"","bid_mode_name":"","bid_mode_status":"","bid_plan_end_date":"","bid_plan_start_date":"","bid_time":"","bidding_cur_name":"","bidding_cur_unit":"","comp_register_no":"","comp_type":"","contact_mobile":"","contact_person":"","continue_bid_price_bid_time":"","cur_contact_mobile":"","cur_contact_person":"","cur_valid_bid_price":"","db_index":"","deposit_amt":"","economic_type":"","fax":"","final_bid_price":"","id":"1450401","id_no":"","ids":[],"industryName":"","linkWeb":"","listing_end_date":"2014-07-03","listing_price":"1400.728732","listing_price_text":"","listing_price_type":"","listing_start_date":"2014-06-04","obj_industry":5,"obj_introduction":"","org_fax":"","org_name":"无锡产权交易所","org_name_detail":"","org_operator_mobile":"","org_operator_name":"","org_principal":"","org_principal_mobile":"","org_web":"www.126.com","phase_order_id":"","phase_status":"","proj_code":"","proj_intro":"","proj_name":"ZGY测试挂牌项目0603-T02","proj_num":0,"proj_type":"2","register_capital":"","sort":"","statutory_representative":"","timing_bid_end_date":"","timing_bid_start_date":"","timing_or_continue_mode":"","trade_mode":"","transfer_code":"","transfer_economic_type":"","transfer_group_code":"","transfer_name":"","transfer_ratio":"","transfer_type":"","type_name":"","url":"","value_added":""},{"address":"","asset_classes":"房产","id":"1449379","listing_end_date":"2014-12-26","listing_price":"3270.000000","listing_start_date":"2014-05-30","obj_industry":0,"org_name":"上海联合产权交易所","org_web":"www.suaee.com","proj_name":"Z国网吉林省电力有限公司白城供电公司部分资产(白城电业综合大厦)","id":"1449215","proj_name":"X国网吉林省电力有限公司白城供电公司部分资产(白城电业综合大厦)","id":"1448794","listing_end_date":"2014-06-13","listing_price":"1200.000000","proj_name":"独立洋房","id":"1448210","listing_end_date":"2014-06-07","listing_price":"17.032000","proj_name":"沪KC2655东风日产轿车一辆","id":"1447026","listing_end_date":"2014-06-06","listing_price":"1.000000","listing_start_date":"2014-05-29","proj_name":"郯城","id":"1446123","listing_price":"8.000000","proj_name":"黑色古董车",交通运输工具,在建工程,其他","id":"1445937","listing_end_date":"2014-06-12","listing_price":"5612.122000","proj_name":"0529上海物权项目02","id":"1445924","listing_price":"57000.000000","proj_name":"0529上海物权项目01","id":"1445069","listing_end_date":"2014-07-08","listing_price":"2.000000","listing_start_date":"2014-05-28","proj_name":"上海黄浦江客轮拍卖","id":"1424095","listing_end_date":"2014-07-14","listing_price":"1400728732.000000","listing_start_date":"2014-05-22","obj_industry":8,"proj_name":"0514-T03测试挂牌项目","id":"1424430","listing_end_date":"2014-06-10","listing_start_date":"2014-05-14","proj_name":"0514-T04测试挂牌项目","id":"1423832","listing_end_date":"2014-06-24","proj_name":"0514-T02测试挂牌项目","id":"1415637","listing_end_date":"2014-09-12","listing_start_date":"2014-05-05","proj_name":"ZGY测试挂牌项目0505-001","id":"1415077","listing_end_date":"2014-09-11","proj_name":"ZGY测试挂牌项目0505-003","id":"1410833","listing_end_date":"2014-09-06","listing_start_date":"2014-04-30","proj_name":"ZGY测试挂牌项目0430-007","id":"1410420","listing_price":"","proj_name":"ZGY测试挂牌项目0430-006","id":"1408988","proj_name":"ZGY测试挂牌项目0430-003","id":"1409036","proj_name":"ZGY测试挂牌项目0430-004","id":"1409220","proj_name":"ZGY测试挂牌项目0430-005","id":"1405392","listing_end_date":"2014-09-05","listing_start_date":"2014-04-29","proj_name":"ZGY测试挂牌项目0429-003","id":"1406822","proj_name":"ZGY测试挂牌项目0429-005","id":"1407355","obj_industry":2,"proj_name":"ZGY测试0429001","id":"1407408","listing_end_date":"2015-02-17","obj_industry":4,"proj_name":"ZGY测试0429002","id":"1405932","proj_name":"ZGY测试挂牌项目0429-004","id":"1396566","listing_end_date":"2014-09-02","listing_price":"1234567890.728732","listing_start_date":"2014-04-25","proj_name":"ZGY测试挂牌项目0425-001","id":"1397475","listing_end_date":"2014-06-11","obj_industry":1,"proj_name":"fdfdfdf","id":"1392305","listing_end_date":"2014-09-01","listing_start_date":"2014-04-24","org_name":"苏州产权交易所","org_web":"","proj_name":"ZGY测试挂牌项目0424-001","id":"1387049","listing_end_date":"2014-08-29","listing_start_date":"2014-04-22","proj_name":"WXCJS1404300003","id":"1374913","listing_end_date":"2014-08-25","listing_start_date":"2014-04-17","proj_name":"ZGY测试挂牌项目0417-001","id":"1368758","listing_end_date":"2014-08-23","listing_start_date":"2014-04-16","proj_name":"ZGY测试挂牌项目0416-004","id":"1368816","proj_name":"ZGY测试挂牌项目0416-005","id":"1368355","proj_name":"ZGY测试挂牌项目0416-001","id":"1368365","proj_name":"ZGY测试挂牌项目0416-002","id":"1368375","proj_name":"ZGY测试挂牌项目0416-003","id":"1370385","listing_end_date":"2014-08-11","listing_price":"0.000090","proj_name":"联交所测试项目0416-001",土地使用权","id":"1370425","obj_industry":10,"proj_name":"联交所测试项目0416-002","id":"1371244","proj_name":"联交所测试项目0416-004","id":"1371772","proj_name":"联交所测试项目0416-005","id":"1369758","listing_end_date":"2014-11-22","obj_industry":11,"proj_name":"上海某地铁站地下商业街经营权","id":"1371230","proj_name":"联交所测试项目0416-003","id":"1362512","listing_end_date":"2014-08-21","listing_start_date":"2014-04-14","proj_name":"ZGY测试挂牌项目0414-005","id":"1362522","proj_name":"ZGY测试挂牌项目0414-006","id":"1355913","proj_name":"ZGY测试挂牌项目0414-002","id":"1362532","proj_name":"ZGY测试挂牌项目0414-007","id":"1355923","proj_name":"ZGY测试挂牌项目0414-003","id":"1355898","proj_name":"ZGY测试挂牌项目0414-001","id":"1314109","listing_end_date":"2014-11-11","listing_price":"0.001111","listing_start_date":"2014-03-28","proj_name":"股权2014032801","id":"1316436","listing_end_date":"2014-07-25","listing_price":"0.000003","proj_name":"中钢贸易有限公司部分资产(上海徐汇区漕溪路250号银海大厦A区801室房产) ","id":"1310007","listing_end_date":"2014-11-20","listing_price":"0.000035","listing_start_date":"2014-03-27","proj_name":"中钢集团上海有限公司部分资产(上海市闸北区长安路1001号长安大厦3号楼24-25层)","id":"1309593","proj_name":"中钢集团","id":"1300098","listing_end_date":"","listing_price":"0.000014","listing_start_date":"2014-03-26","org_name":"南京文化产权交易所","proj_name":"ZGY测试挂牌项目0326-01","id":"1299242","listing_end_date":"2014-07-15","listing_price":"0.000000","listing_start_date":"2014-03-25","proj_name":"thiscase","id":"1296552","listing_start_date":"2014-03-24","proj_name":"南京文化测试挂牌项目0324-01","id":"1291955","listing_start_date":"2014-03-21","org_name":"南京产权交易所","proj_name":"ZGY测试挂牌项目0321-001","id":"1288170","listing_start_date":"2014-03-19","proj_name":"ZGY测试挂牌项目0319-01","id":"1281305","listing_start_date":"2014-03-14","proj_name":"ZGY测试挂牌项目0314-01","id":"1282791","proj_name":"3","id":"1279279","listing_price":"0.000025","listing_start_date":"2014-03-13","proj_name":"上海皇庭房地产部分资产",设备","id":"1278581","listing_start_date":"2014-03-12","proj_name":"测试挂牌项目0312-03","id":"1278567","proj_name":"测试挂牌项目0312-02","id":"1272393","listing_price":"0.000001","listing_start_date":"2014-03-11","proj_name":"20140311","id":"1271308","listing_price":"0.000010","proj_name":"test25257759","id":"1270440","listing_start_date":"2014-03-10","proj_name":"ZGY测试挂牌项目031001","id":"1269391","proj_name":"0307-002ZGY测试项目91209","id":"1269311","listing_start_date":"2014-03-07","proj_name":"0307-001ZGY测试项目09121","id":"1269400","proj_name":"0304-001ZGY","id":"1266665","listing_start_date":"2014-03-06","proj_name":"0306-001ZGY","id":"1268111","proj_name":"test-q","id":"1264240","listing_start_date":"2014-03-03","proj_name":"0303-003ZGY测试","id":"1264250","id":"1260824","listing_start_date":"2014-03-01","proj_name":"上海市松江区泗泾镇泗砖路1弄143号","id":"1260919","proj_name":"ZGY测试项目0301-0001","id":"1260084","listing_start_date":"2014-02-27","org_name":"","id":"1260150","proj_name":"测试测试测试测试测试测试测试测试测试测试","id":"1260031","listing_price":"0.000002","proj_name":"ZGY挂牌测试项目0227-001","id":"1260263","proj_name":"武汉长江航运中心实业有限公司23%股权","id":"1260303","proj_name":"ZGY挂牌测试项目0227-003","value_added":""}]]
解析代码如下:
<pre class="java" name="code">public class JsonParse { public static void main(String[] args) throws Exception { String jsonString = ClientAbortMethod.getJsonString(null); String strAfter = jsonString.substring(2,jsonString.length() - 4); System.out.println(strAfter); Bean obj = JSON.parSEObject(strAfter,Bean.class); System.out.println(obj); } }