实现目标
读取文件中的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/undeuxtrois@2x.png"},
代码实现
1、实体类
importjava.util.List;publicclassImportBrand
{PRivateintid;privateString name;privateString has_barcode;privateString barcode_format;privateString homepage;privateList<String>regions;privateList<String>other_stores;privateList<String>typos;privateString logo;privateString android_banner_url;privateString ios_banner_url;privateString ios_logo_url;
@OverridepublicString toString()
{//TODO Auto-generated method stubreturn"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;
}publicintgetId()
{returnid;
}publicvoidsetId(intid)
{this.id =id;
}publicString getName()
{returnname;
}publicvoidsetName(String name)
{this.name =name;
}publicString getHas_barcode()
{returnhas_barcode;
}publicvoidsetHas_barcode(String has_barcode)
{this.has_barcode =has_barcode;
}publicString getBarcode_format()
{returnbarcode_format;
}publicvoidsetBarcode_format(String barcode_format)
{this.barcode_format =barcode_format;
}publicString getHomepage()
{returnhomepage;
}publicvoidsetHomepage(String homepage)
{this.homepage =homepage;
}publicList<String>getRegions()
{returnregions;
}publicvoidsetRegions(List<String>regions)
{this.regions =regions;
}publicList<String>getOther_stores()
{returnother_stores;
}publicvoidsetOther_stores(List<String>other_stores)
{this.other_stores =other_stores;
}publicList<String>getTypos()
{returntypos;
}publicvoidsetTypos(List<String>typos)
{this.typos =typos;
}publicString getLogo()
{returnlogo;
}publicvoidsetLogo(String logo)
{this.logo =logo;
}publicString getAndroid_banner_url()
{returnandroid_banner_url;
}publicvoidsetAndroid_banner_url(String android_banner_url)
{this.android_banner_url =android_banner_url;
}publicString getIos_banner_url()
{returnios_banner_url;
}publicvoidsetIos_banner_url(String ios_banner_url)
{this.ios_banner_url =ios_banner_url;
}publicString getIos_logo_url()
{returnios_logo_url;
}publicvoidsetIos_logo_url(String ios_logo_url)
{this.ios_logo_url =ios_logo_url;
}
}
2、读取文件
importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.List;/*** 读取文件
*
*@authorzcr
**/publicclassImportFile
{/*** 功能:Java读取txt文件的内容 步骤:1:先获得文件句柄 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流 4:一行一行的输出。readline()。 备注:需要考虑的是异常情况
*
*@paramfilePath
* 文件路径[到达文件:如: D:\aa.txt]
*@return将这个文件按照每一行切割成数组存放到list中。*/publicstaticList<String>readTxtFileIntoStringArrList(String filePath)
{
List<String> list =newArrayList<String>();try{
String encoding= "UTF-8";
File file=newFile(filePath);if(file.isFile() &&file.exists())
{//判断文件是否存在InputStreamReader read =newInputStreamReader(newFileInputStream(file), encoding);//考虑到编码格式BufferedReader bufferedReader =newBufferedReader(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();
}returnlist;
}publicstaticvoidmain(String argv[])
{
String filePath= "C:\\Users\\owner\\Desktop\\卓信科技实习\\stores.json";
List<String> dataList =readTxtFileIntoStringArrList(filePath);for(inti = 0 ; i < dataList.size() ; i ++)
{
System.out.println(dataList.get(i));
}
System.out.println(dataList.size());
}/*** 判断数据是否是合法的格式
*@paramjsonStr 带判断的数据
*@return返回该行是否是正确的格式*/publicstaticbooleanisRightFormat(String jsonStr)
{returnjsonStr.matches("[\\p{Space}]*[{]{1}.*[}]{1}[,]{1}");
}
}
3、方法调用及测试
/*** 格式化json数据
*@authorzcr
**/publicclassFormatJson
{publicstaticvoidmain(String[] args)
{String filePath= "C:\\Users\\owner\\Desktop\\卓信科技实习\\stores.json";List<ImportBrand> brandList =FormatJson.formatFileListToBrand(filePath);for(inti = 0 ; i < brandList.size() ; i ++)
{
System.out.println(brandList.get(i));
}
System.out.println(brandList.size());
}/*** 将json格式数据转换成Import对象
*@paramjsonStr 带转换的json对象
*@returnjson格式数据对应的对象*/publicstaticImportBrand formatFromJsonToObject(String jsonStr)
{
Gson gson=newGson();
ImportBrand brand= gson.fromJson(jsonStr,ImportBrand.class);return
brand;
}/*** 将String类型的json格式转换为ImportBrand类型的集合
*@paramjsonStrList 待转换的json格式List对象
*@returnjson格式对象转换而成的ImportBrand对象集合*/publicstaticList<ImportBrand> formatStringListToBrand(List<String>jsonStrList)
{
List<ImportBrand> listImportBrand =newArrayList<ImportBrand>();intsize =jsonStrList.size();for(inti = 0 ; i < size ; i ++)
{
listImportBrand.add(formatFromJsonToObject(jsonStrList.get(i)));
}returnlistImportBrand;
}/*** 读取文件,将json格式的数据转换成List对象的ImportBrand
*@paramfilePath 读取的文件路径
*@return*/publicstaticList<ImportBrand>formatFileListToBrand(String filePath)
{
List<String> dataList =ImportFile.readTxtFileIntoStringArrList(filePath);
List<ImportBrand> brandList =formatStringListToBrand(dataList);return
brandList;
}
}
致谢:感谢您的阅读.