Txt文档解析(Json字符串),Json字符串转List,并存库
程序员文章站
2022-04-15 18:36:45
Json数据格式:[{"number_id":"job","score":"39"},{"number_id":"tom","score":"30"},{"number_id":"sha","score":"32"}]需要引入的jar包:1、commons-collections-3.2.1.jar2、json-lib-2.4-jdk15.jar总结:从Txt文档读取的Json转为List步骤:1、读取文件2、解析json数据,将Json字符串转为Map3、将Map转为Li....
Json数据格式:[{"number_id":"job","score":"39"},{"number_id":"tom","score":"30"},{"number_id":"sha","score":"32"}]
需要引入的jar包:
1、commons-collections-3.2.1.jar
2、json-lib-2.4-jdk15.jar
总结:
从Txt文档读取的Json转为List步骤:
1、读取文件
2、解析json数据,将Json字符串转为Map
3、将Map转为List
4、循环List,拿到需要的数据,进行数据库操作
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.collections.map.ListOrderedMap; import net.sf.json.JSONArray; import net.sf.json.JSONObject; //以上为用到的jar包 public static void main(String[] args) throws IOException { String filepath = "D:\\2.txt"; String readJsonData = readJsonData(filepath); List<Map<String,Object>> parseJSON2List = parseJSON2List(readJsonData); for (int i = 0; i < parseJSON2List.size(); i++) { Map<String, Object> map = parseJSON2List.get(i); String number_id = String.valueOf(map.get("number_id")); String score = String.valueOf(map.get("score")); System.out.println("number_id====="+number_id+"-----------score==="+score); //打印结果 //number_id=====job-----------score===39 //number_id=====tom-----------score===30 //number_id=====sha-----------score===32 } }
/** * 读取Txt文档 * @param pactFile * @return * @throws IOException */ public static String readJsonData(String filepath) throws IOException { // 读取文件数据 StringBuffer strbuffer = new StringBuffer(); File file= new File(filepath); if (!file.exists()) { System.err.println(filepath+"文件不存在"); } try { FileInputStream fis = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(fis, "UTF-8"); BufferedReader in = new BufferedReader(inputStreamReader); String str; while ((str = in.readLine()) != null) { strbuffer.append(str); } in.close(); } catch (IOException e) { e.getStackTrace(); } //System.out.println("读取文件结束util"); return strbuffer.toString(); }
/** * 将Map转为为list * @param json * @return */ public static List<Map<String, Object>> parseJSON2List(String json) { JSONArray jsonArr = JSONArray.fromObject(json); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator<JSONObject> it = jsonArr.iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2.toString())); } return list; }
/** * 解析Json数据 * @param jsonStr * @return */ public static Map<String, Object> parseJSON2Map(String jsonStr) { ListOrderedMap map = new ListOrderedMap(); // 最外层解析 JSONObject json = JSONObject.fromObject(jsonStr); for (Object k : json.keySet()) { Object v = json.get(k); // 如果内层还是数组的话,继续解析 if (v instanceof JSONArray) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator<JSONObject> it = ((JSONArray) v).iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2.toString())); } map.put(k.toString(), list); } else { map.put(k.toString(), v); } } Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); Object object = map.get(key); // 进行遍历删除,当值为空的时候删除这条数据 if (object.equals("")) { iterator.remove(); map.remove(key); } } return map; }
参考链接:https://blog.csdn.net/qq_42262444/article/details/86292315
本文地址:https://blog.csdn.net/JerryBal/article/details/110223142
上一篇: Java自动化测试系列[v1.0.0][关键字驱动]
下一篇: feign集成Hystrix