實現目標
讀取文件中的json格式數據,一行為一條json格式數據。進行解析封裝成實體類。
通過google的Gson對象解析json格式數據
我現在解析的json格式數據為:
{"id": "1403","name": "1.2.3 Diva","has_barcode": true,"barcode_format": "EAN_13","homepage": "http://1-2-3.fr","regions": ["DE","FR"],"other_stores": [],"typos": ["un deux trois","un1deux2trois3"],"logo": "undeuxtrois","android_banner_url": "http://stocardapp.s3-external-3.amazonaws.com/android/banner/undeuxtrois.png","ios_banner_url": "http://stocardapp.s3-external-3.amazonaws.com/ios/banners/undeuxtrois.png","ios_logo_url": "http://stocardapp.s3-external-3.amazonaws.com/ios/icons/[email protected]"},
代碼實現
1、實體類
import java.util.List;public class ImportBrand{ PRivate int id; private String name; private String has_barcode; private String barcode_format; private String homepage; private List<String> regions; private List<String> other_stores; private List<String> typos; private String logo; private String android_banner_url; private String ios_banner_url; private String ios_logo_url; @Override public String toString() { // TODO Auto-generated method stub return "id=" + id + ",name = " + name + ",has_barcode = " + has_barcode + ",barcode_format=" + barcode_format + ",homepage =" + homepage + ",regions = " + regions +",logo = " + logo +",android_banner_url = " + android_banner_url + ",ios_banner_url=" + ios_banner_url + ",ios_logo_url = " + ios_logo_url; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHas_barcode() { return has_barcode; } public void setHas_barcode(String has_barcode) { this.has_barcode = has_barcode; } public String getBarcode_format() { return barcode_format; } public void setBarcode_format(String barcode_format) { this.barcode_format = barcode_format; } public String getHomepage() { return homepage; } public void setHomepage(String homepage) { this.homepage = homepage; } public List<String> getRegions() { return regions; } public void setRegions(List<String> regions) { this.regions = regions; } public List<String> getOther_stores() { return other_stores; } public void setOther_stores(List<String> other_stores) { this.other_stores = other_stores; } public List<String> getTypos() { return typos; } public void setTypos(List<String> typos) { this.typos = typos; } public String getLogo() { return logo; } public void setLogo(String logo) { this.logo = logo; } public String getAndroid_banner_url() { return android_banner_url; } public void setAndroid_banner_url(String android_banner_url) { this.android_banner_url = android_banner_url; } public String getIos_banner_url() { return ios_banner_url; } public void setIos_banner_url(String ios_banner_url) { this.ios_banner_url = ios_banner_url; } public String getIos_logo_url() { return ios_logo_url; } public void setIos_logo_url(String ios_logo_url) { this.ios_logo_url = ios_logo_url; }}
2、讀取文件
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;/** * 讀取文件 * * @author zcr * */public class ImportFile{ /** * 功能:Java讀取txt文件的內容 步驟:1:先獲得文件句柄 2:獲得文件句柄當做是輸入一個字節碼流,需要對這個輸入流進行讀取 * 3:讀取到輸入流后,需要讀取生成字節流 4:一行一行的輸出。readline()。 備注:需要考慮的是異常情況 * * @param filePath * 文件路徑[到達文件:如: D:/aa.txt] * @return 將這個文件按照每一行切割成數組存放到list中。 */ public static List<String> readTxtFileIntoStringArrList(String filePath) { List<String> list = new ArrayList<String>(); try { String encoding = "UTF-8"; File file = new File(filePath); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { if (isRightFormat(lineTxt)) { list.add(lineTxt.substring(lineTxt.indexOf("{"),lineTxt.lastIndexOf(','))); } } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("讀取文件內容出錯"); e.printStackTrace(); } return list; } public static void main(String argv[]) { String filePath = "C://Users//owner//Desktop//卓信科技實習//stores.json"; List<String> dataList = readTxtFileIntoStringArrList(filePath); for(int i = 0 ; i < dataList.size() ; i ++) { System.out.println(dataList.get(i)); } System.out.println(dataList.size()); } /** * 判斷數據是否是合法的格式 * @param jsonStr 帶判斷的數據 * @return 返回該行是否是正確的格式 */ public static boolean isRightFormat(String jsonStr) { return jsonStr.matches("[//p{Space}]*[{]{1}.*[}]{1}[,]{1}"); }}
3、方法調用及測試
/** * 格式化json數據 * @author zcr * */public class FormatJson{ public static void main(String[] args) { String filePath = "C://Users//owner//Desktop//卓信科技實習//stores.json"; List<ImportBrand> brandList = FormatJson.formatFileListToBrand(filePath); for(int i = 0 ; i < brandList.size() ; i ++) { System.out.println(brandList.get(i)); } System.out.println(brandList.size()); } /** * 將json格式數據轉換成Import對象 * @param jsonStr 帶轉換的json對象 * @return json格式數據對應的對象 */ public static ImportBrand formatFromJsonToObject(String jsonStr) { Gson gson = new Gson(); ImportBrand brand = gson.fromJson(jsonStr,ImportBrand.class); return brand; } /** * 將String類型的json格式轉換為ImportBrand類型的集合 * @param jsonStrList 待轉換的json格式List對象 * @return json格式對象轉換而成的ImportBrand對象集合 */ public static List<ImportBrand> formatStringListToBrand(List<String> jsonStrList) { List<ImportBrand> listImportBrand = new ArrayList<ImportBrand>(); int size = jsonStrList.size(); for(int i = 0 ; i < size ; i ++) { listImportBrand.add(formatFromJsonToObject(jsonStrList.get(i))); } return listImportBrand; } /** * 讀取文件,將json格式的數據轉換成List對象的ImportBrand * @param filePath 讀取的文件路徑 * @return */ public static List<ImportBrand> formatFileListToBrand(String filePath) { List<String> dataList = ImportFile.readTxtFileIntoStringArrList(filePath); List<ImportBrand> brandList = formatStringListToBrand(dataList); return brandList; }}
致謝:感謝您的閱讀.
新聞熱點
疑難解答