引入是要的maven依赖:

 <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>

转换:

 package com.iot.zjdy.exampl.test;

 import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter; /**
* Created by Yanwu 2018/2/8.
*/
public class JsonToSqlTest {
private static final String PATH = "C:\\demo\\file\\Cab_LN_FeaturesToJSON.json"; public static void main(String[] args) throws Exception {
System.out.println("========== JSON ---> 转换成 SQL 开始 ==========");
jsonToExcel();
System.out.println("========== JSON ---> 转换成 SQL 结束 ==========");
} private static void jsonToExcel() throws Exception {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject) jsonParser.parse(new FileReader(PATH));
JsonElement features = jsonObject.get("features");
JsonArray asJsonArray = features.getAsJsonArray();
for (int i = 0; i < asJsonArray.size(); i++) {
JsonElement jsonElement = asJsonArray.get(i);
JsonObject featuresObj = jsonElement.getAsJsonObject();
JsonElement attributes = featuresObj.get("attributes");
JsonObject attributesObj = attributes.getAsJsonObject();
JsonElement swchName = attributesObj.get("swchName");
String nameStr = swchName.toString();
JsonElement geometry = featuresObj.get("geometry");
JsonObject geometryObj = geometry.getAsJsonObject();
JsonElement path = geometryObj.get("paths");
String pathStr = path.toString();
String replace = nameStr.replace("\"", "");
String sqlStr = "insert into poly_line (id, box_name, paths) values (" + i + ", '" + replace + "', '" + pathStr + "'); \r\n";
System.out.println(sqlStr);
File file = new File("C:\\demo\\file\\Cab_LN.sql");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(sqlStr);
bufferedWriter.close();
}
}
}

将json文件转换成insert语句的sql文件的更多相关文章

  1. PL/Sql快速执行 insert语句的.sql文件

    当全是 insert语句的.sql文件太大时(insert 语句条数太大),直接打开执行sql文件,pl/sql会卡死. 这是可以用pl/sql的命令窗口来执行.sql文件,操作步骤如下: 1.新建命 ...

  2. 如何将md文件转换成带目录的html文件

    配置环境node 去官网下一个node安装包,下一步下一步: 由于现在的node都自带npm,直接 npm install i5ting_toc 这样安装好了i5ting_toc这个包, 进入你实现准 ...

  3. net9:图片文件转换成二进制流存入SQL数据库,以及从数据库中读取二进制流输出文件

    原文发布时间为:2008-08-10 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  4. sql server 数据库导出表里所有数据成insert 语句

    有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...

  5. 将jar文件转换成exe可执行文件[转]

    将jar文件转换成exe可执行文件: exe文件使用方便,而且还可以提高源码及资源的安全性,但同时也失去了java的初衷--跨平台性. 如果你坚持要转换成exe文件,请按以下方式进行: 利用exe4j ...

  6. JAVA将文件转换成byte数组(byte[])

    /** * 将文件转换成byte数组 * @param filePath 文件File类 通过new File(文件路径) * @return byte数组 */ public static byte ...

  7. java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句

    package com; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impo ...

  8. json串转化成xml文件、xml文件转换成json串

    1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...

  9. MyBatis项目实战 快速将MySQL转换成Oracle语句

    一.前言 因项目需求,小编要将项目从mysql迁移到oracle中 ~ 之前已经完成 数据迁移 (https://zhengqing.blog.csdn.net/article/details/103 ...

随机推荐

  1. 单源最短路径 Bellman_ford 和 dijkstra

    首先两个算法都是常用于 求单源最短路径 关键部分就在于松弛操作 实际上就是dp的感觉 if (dist[e.to] > dist[v] + e.cost) { dist[e.to] = dist ...

  2. 显示倒计时,为零时自动点击按钮提交【JavaScript实现】

    原文发布时间为:2008-10-17 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <title>显示倒计时,完毕提交</tit ...

  3. 学.net必学的东西 10项【不知道我能不能学这么多,!- -,光程序编辑我都累死了】

    原文发布时间为:2008-10-30 -- 来源于本人的百度文章 [由搬家工具导入] 10项.NET必学的技术2007年08月28日 星期二 14:58 1、WCF (Windows Communic ...

  4. hdu 4049 Tourism Planning [ 状压dp ]

    传送门 Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. THUPC2018看题总结

    THUPC2018看题总结 #6387. 「THUPC2018」绿绿与串串 / String 据说是签到题啊. 首先根据题目的意思,我们发现如果能找到那个最后一次选择的对称轴岂不是美滋滋. 自然地,我 ...

  6. Hibernate学习笔记(三)

    我是从b站视频上学习的hibernate框架,其中有很多和当前版本不符合之处,我在笔记中进行了修改以下是b站视频地址:https://www.bilibili.com/video/av14626440 ...

  7. Mysql数据库的事物

    一 .事物的特性:ACID 数据库的事务必须具备ACID特性,ACID是指 Atomicity(原子性).Consistensy(一致性).Isolation(隔离型)和Durability(持久性) ...

  8. Meteor ToDo App实例

    在本章中,我们将创建一个简单的待办事项应用程序. 第1步 - 创建应用程序 打开命令提示符,运行以下命令 - C:\Users\Administrator\Desktop>meteor crea ...

  9. 【c++】面向对象程序设计之访问控制与继承

    受保护的成员(protected): 1.和私有成员类似,受保护的成员对于类的用户来说是不可访问的 2.和共有成员类似,受保护的成员对于派生类的成员和友元来说是可访问的 3.派生类的友元只能通过派生类 ...

  10. 【APUE】vim常用命令

    转自:http://coolshell.cn/articles/5426.html 基本命令: i → Insert 模式,按 ESC 回到 Normal 模式. x → 删当前光标所在的一个字符. ...