| 12 | |
| 13 | |
| 14 | public class Map2Json { |
| 15 | private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 16 | private static int i = 1 ; |
| 17 | |
| 18 | public int getI() { |
| 19 | return i; |
| 20 | } |
| 21 | |
| 22 | public void setI(int i) { |
| 23 | this.i = i; |
| 24 | } |
| 25 | |
| 26 | //map转json |
| 27 | public static String toJson(Map<Long, Node> map) { |
| 28 | if (map == null) |
| 29 | map = new HashMap<Long, Node>(); |
| 30 | return gson.toJson(map); |
| 31 | } |
| 32 | public static String toJson1(Map<String, List> map) { |
| 33 | if (map == null) |
| 34 | map = new HashMap<>(); |
| 35 | return gson.toJson(map); |
| 36 | } |
| 37 | |
| 38 | public static String toJson2(Map<String, Object> map) { |
| 39 | if (map == null) |
| 40 | map = new HashMap<String, Object>(); |
| 41 | return gson.toJson(map); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | //写入json文件 |
| 46 | public static boolean createJsonFile(String jsonString, String filePath, String fileName) { |
| 47 | boolean flag = true; |
| 48 | |
| 49 | Map2Json map2Json = new Map2Json(); |
| 50 | int i = map2Json.getI(); |
| 51 | //System.out.println(i); |
| 52 | |
| 53 | |
| 54 | String fullPath = filePath + File.separator + fileName+ i + ".json"; |
| 55 | i++; |
| 56 | //System.out.println(i); |
| 57 | map2Json.setI(i); |
| 58 | |
| 59 | try{ |
| 60 | File file = new File(fullPath); |
| 61 | if(!file.exists()); |
| 62 | file.createNewFile(); |
| 63 | |
| 64 | Writer write = new OutputStreamWriter(new FileOutputStream(file),"UTF-8"); |
| 65 | write.write(jsonString); |
| 66 | |
| 67 | write.flush(); |
| 68 | write.close(); |
| 69 | |
| 70 | }catch (Exception e){ |
| 71 | flag = false; |
nothing calls this directly
no outgoing calls
no test coverage detected