實作目标
讀取檔案中的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;
}
}
緻謝:感謝您的閱讀.