如何根據(jù)json數(shù)據(jù)的url、json數(shù)據(jù)里的鍵名玩徊,生成自定義數(shù)據(jù)集并生成皕杰報(bào)表頁(yè)面?
準(zhǔn)備工作:
Demo:jsonurl:https://api.douban.com/v2/movie/top250?start=0&count=10
Json鍵名:name,id,alt
Json字符串解析工具:Fastjson
解決方案:
1.? ? ? ?新建皕杰報(bào)表自定義數(shù)據(jù)集類需要集成bios.report.api.customize.ArrayDataSet類
package? bios.report.dev.examples.dataset;
public class MyJSONDataSet implements ArrayDataSet {
??? //新建數(shù)據(jù)集字段名
??? private static String[] metaData;
??? //新建list存儲(chǔ)Object數(shù)組
??? private static List<Object[]> data =new ArrayList<Object[]>();;
??? @Override
??? //
? 獲取數(shù)據(jù)集中傳遞的參數(shù)值
??? public void applyParams(Object[] arg0) {
??????? //獲取數(shù)據(jù)集傳參的jsonurl
??????? String url = arg0[0].toString();
??????? //獲取數(shù)據(jù)集傳參的鍵名
??????? String fields =arg0[1].toString();
??????? metaData = new String[] {};
??????? //傳遞的鍵名是字符串需要拆分成字符串組
??????? metaData = fields.split(",");
??????? getjSON(url, metaData);
??? }
??? @Override
??? //
? 返回?cái)?shù)據(jù)集字段名
??? public String[]
? getMetaData() {
??????? return metaData;
??? }
??? @Override
??? //
? 返回?cái)?shù)據(jù)集數(shù)據(jù)長(zhǎng)度
??? public int getRowCount() {
??????? return data.size();
??? }
??? @Override
??? //
? 返回?cái)?shù)據(jù)集中指定行的數(shù)據(jù)
??? public Object[] getRowData(int rowIndex) {
??????? return (Object[]) data.get(rowIndex);
??? }
??? //
? 根據(jù)傳遞到URL獲取json數(shù)據(jù)
??? public static void getjSON(String url, String[] fields) {
??????? String res = "";
??????? StringBuffer buffer = new StringBuffer();
??????? try {
??????????? URL urls = new URL(url);
??????????? HttpURLConnection urlCon = (HttpURLConnection)
? urls.openConnection();
??????????? if (200 == urlCon.getResponseCode()) {
??????????????? InputStream is = urlCon.getInputStream();
??????????????? InputStreamReader isr = new InputStreamReader(is, "utf-8");
??????????????? BufferedReader br = new BufferedReader(isr);
??????????????? String str = null;
??????????????? while ((str = br.readLine()) != null) {
??????????????????? buffer.append(str);
??????????????? }
??????????????? br.close();
??????????????? isr.close();
??????????????? is.close();
??????????????? res = buffer.toString();
??????????????? parseJsonString(res, fields);
??????????? }
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??? }
??? //解析json字符串
??? public static void? parseJsonString(String json, String[] fields) {
??????? LinkedHashMap<String, Object> jsonMap= JSON.parseObject(json,
??????????????? new? TypeReference<LinkedHashMap<String, Object>>() {
??????????????? });
??????? for (Map.Entry<String,
? Object> entry : jsonMap.entrySet()) {
??????????? parseJsonMap(entry, fields);
??????? }
??? }
??? /*
??? ?* 1、循環(huán)解析json
??? ?* 2、判斷鍵名在json哪一層
??? ?* 3躺彬、根據(jù)鍵名獲取數(shù)據(jù)并存儲(chǔ)到數(shù)組中
??? ?* 4、將數(shù)組存儲(chǔ)到list中
??? ?*/
??? public static void? parseJsonMap(Map.Entry<String, Object> entry,String[] fields) {
??????? //
? 如果是單個(gè)map繼續(xù)遍歷
??????? if (entry.getValue() instanceof Map) {
??????????? LinkedHashMap<String, Object> jsonMap= JSON.parseObject(entry.getValue().toString(),
??????????????????? new? TypeReference<LinkedHashMap<String, Object>>() {
??????????????????? });
??????????? for (Map.Entry<String,
? Object> entry2 : jsonMap.entrySet()) {
??????????????? parseJsonMap(entry2,fields);
??????????? }
??????? }
??????? //
? 如果是list就提取出來
??????? if (entry.getValue() instanceof List) {
??????????? Listlist=? (List)entry.getValue();
??????????? for (int i = 0; i < list.size(); i++) {
??????????????? // 如何還有梅惯,循環(huán)提取
??????????????? if(list.get(i).toString().contains("[") || (list.get(i).toString().contains("{"))) {
??????????????????? LinkedHashMap<String, Object> jsonMap= JSON.parseObject(list.get(i).toString(),
??????????????????????????? new? TypeReference<LinkedHashMap<String, Object>>() {
??????????????????????????? });
??????????????????? Object[] str1=new Object[fields.length];
??????????????????? for (Map.Entry<String,
? Object> entry1 : jsonMap.entrySet()) {
??????????????????????? if(Arrays.asList(fields).contains(entry1.getKey())) {
??????????????????????????? JSONObject jObject=JSON.parseObject(list.get(i).toString());
??????????????????????????? String jString=JSONObject.toJSONString(jObject,SerializerFeature.WriteMapNullValue);
??????????????????????????? JSONObject jObject2=JSON.parseObject(jString);
??????????????????????????? for (int j = 0; j < fields.length; j++) {
??????????????????????????????? if(jObject2.getString(fields[j])==null) {
??????????????????????????????????? str1[j]="";
??????????????????????????????? }else {
??????????????????????????????????? str1[j]=jObject2.getString(fields[j]);
??????????????????????????????? }
??????????????????????????? }
??????????????????????? }else {
??????????????????????????? parseJsonString(list.get(i).toString(), fields);
??????????????????????? }
??????????????????? }
??????????????????? data.add(str1);
??????????????? }
??????????? }
??????? }
??? }
}
2.? ? ? ? 將自定義數(shù)據(jù)集類導(dǎo)出jar包
3.? ? ? 設(shè)計(jì)器中將自定義數(shù)據(jù)集jar包和fastjsonjar包導(dǎo)入進(jìn)來
4.? ? ? ? 新建報(bào)表-新建參數(shù)url和fields
參數(shù)url:字符串類型宪拥、jsonurl
參數(shù)fields:字符串類型、json鍵名
5.? ? ? ? 新建數(shù)據(jù)集铣减,數(shù)據(jù)源不選擇她君、類型為自定義類型
編輯查詢sql,寫這個(gè)自定義數(shù)據(jù)集類的package.class
選擇數(shù)據(jù)集參數(shù)葫哗,將需要傳參的參數(shù)設(shè)置上去(參數(shù)順序和自定義數(shù)據(jù)集類中的順序一致)
6.? ? ? ? 報(bào)表設(shè)計(jì)
A3表達(dá)式 =ds.select(#0) ,擴(kuò)展方向?yàn)榭v向擴(kuò)展
B2表達(dá)式 =parting(@fields,','),擴(kuò)展方向?yàn)闄M向擴(kuò)展
B3表達(dá)式=eval("ds.getone("+B2+",#0=A3)")
7.? ? ?Web預(yù)覽
8.? ? ? 讀取json數(shù)據(jù)成功缔刹。
9.? ? ?將brt文件、fastjson jar包和自定義數(shù)據(jù)集jar包復(fù)制到 web應(yīng)用中劣针。