实现目标
读取文件中的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:[email protected].png"},
代码实现
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; } }
致谢:感谢您的阅读.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/15129.html