• 将json文件转换成insert语句的sql文件


    引入是要的maven依赖:

    1 <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    2 <dependency>
    3     <groupId>com.google.code.gson</groupId>
    4     <artifactId>gson</artifactId>
    5     <version>2.2.4</version>
    6 </dependency>

    转换:

     1 package com.iot.zjdy.exampl.test;
     2 
     3 import com.google.gson.JsonArray;
     4 import com.google.gson.JsonElement;
     5 import com.google.gson.JsonObject;
     6 import com.google.gson.JsonParser;
     7 import java.io.BufferedWriter;
     8 import java.io.File;
     9 import java.io.FileReader;
    10 import java.io.FileWriter;
    11 
    12 
    13 /**
    14  * Created by Yanwu 2018/2/8.
    15  */
    16 public class JsonToSqlTest {
    17     private static final String PATH = "C:\demo\file\Cab_LN_FeaturesToJSON.json";
    18 
    19     public static void main(String[] args) throws Exception {
    20         System.out.println("========== JSON ---> 转换成 SQL 开始 ==========");
    21         jsonToExcel();
    22         System.out.println("========== JSON ---> 转换成 SQL 结束 ==========");
    23 }
    24 
    25     private static void jsonToExcel() throws Exception {
    26         JsonParser jsonParser = new JsonParser();
    27         JsonObject jsonObject = (JsonObject) jsonParser.parse(new FileReader(PATH));
    28         JsonElement features = jsonObject.get("features");
    29         JsonArray asJsonArray = features.getAsJsonArray();
    30         for (int i = 0; i < asJsonArray.size(); i++) {
    31             JsonElement jsonElement = asJsonArray.get(i);
    32             JsonObject featuresObj = jsonElement.getAsJsonObject();
    33             JsonElement attributes = featuresObj.get("attributes");
    34             JsonObject attributesObj = attributes.getAsJsonObject();
    35             JsonElement swchName = attributesObj.get("swchName");
    36             String nameStr = swchName.toString();
    37             JsonElement geometry = featuresObj.get("geometry");
    38             JsonObject geometryObj = geometry.getAsJsonObject();
    39             JsonElement path = geometryObj.get("paths");
    40             String pathStr = path.toString();
    41             String replace = nameStr.replace(""", "");
    42             String sqlStr = "insert into poly_line (id, box_name, paths) values (" + i + ", '" + replace + "', '" + pathStr + "'); 
    ";
    43             System.out.println(sqlStr);
    44             File file = new File("C:\demo\file\Cab_LN.sql");
    45             if (!file.exists()) {
    46                 file.createNewFile();
    47             }
    48             FileWriter fileWriter = new FileWriter(file, true);
    49             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    50             bufferedWriter.write(sqlStr);
    51             bufferedWriter.close();
    52         }
    53     }
    54 }
  • 相关阅读:
    符号解析
    编译器与链接器的功能
    hook的本质就是在本原可执行文件中加东西
    Mac-O文件加载的全过程(一)
    系统在执行可执行文件几个过程
    动态库连接器–动态库链接信息(Mach-O文件格式和程序从加载到执行过程)
    load 调用的顺序
    iPhone Mach-O文件格式与代码签名
    Redis正确使用的十个技巧
    redis slowlog
  • 原文地址:https://www.cnblogs.com/yanwu0527/p/8432145.html
Copyright © 2020-2023  润新知