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; } }致谢:感谢您的阅读.
://www.bkjia.com/Javabc/1072365.htmlTechArticle解析json格式数据,解析json格式 实现目标 读取文件中的json格式数据,一行为一条json格式数据。进行解析封装成实体类。 通过google的Gson对...