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

摘要:
要导入的Maven依赖项:1&lt--https://mvnrepository.com/artifact/com.google.code.gson/gson--&gt ; 2<依赖性>3<组ID>com.google.code。gson</组ID>4<artifactId>gson</artifactId>

引入是要的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 }

免责声明:文章转载自《将json文件转换成insert语句的sql文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇苹果手机Chrome浏览器显示input:disabled时字体颜色总是为浅灰色uniapp、Vue组件的使用引用子组件、传值下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JSON数据的处理中的特殊字符

     JSON如今是非经常见的处理数据的方式了。但因为自己使用的是反射获取数据,必须自己处理特殊字符,但总是发现有一些看不见的字符在前台 var obj = jQuery.parseJSON(msg);会转换失败。     比如例如以下在Vs中能够看到仅仅有两个字符    可实际上却有三个字符,使用notepad++打开 一直不明确这些字符是怎样进...

大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)

以下代码中所注释的部分是所改版的地方。:)Krystalware.SlickUpload.dll/Files/bigmouthz/SlickUpload.rar------------------------------------------------------using System;using System.Collections;using S...

Java操作PDF,在PDF文件指定位置输出水印

需要参考我的上一篇博客,定位PDF中的关键字,找出需要打印水印的坐标位置。 先说测试结果(PDF原件也是上一篇中的图片所示): 新生成的带有水印的PDF文件如下所示: junit测试代码及输出: maven配置文件 <!--引入pdf --> <dependency> <groupId>com.i...

C#操作Xml:XmlSerializer 对象的Xml序列化和反序列化

这篇随笔对应的.Net命名空间是System.Xml.Serialization;文中的示例代码需要引用这个命名空间。 为什么要做序列化和反序列化? .Net程序执行时,对象都驻留在内存中;内存中的对象如果需要传递给其他系统使用;或者在关机时需要保存下来以便下次再次启动程序使用就需要序列化和反序列化。 范围:本文只介绍xml序列化,其实序列化可以是二进...

ASP.NET基础知识整理

Asp.net六大对象 1.Request-->读取客户端在Web请求期间发送的值 常用方法: 1、Request.UrlReferrer请求的来源,可以根据这个判断从百度搜的哪个关键词、防下载盗链、防图片盗链,可以伪造(比如迅雷)。 (使用全局一般处理程序) 2、Request.UserHostAddress获得访问者的IP地址 3、Reque...

FLINK基础(107): DS算子与窗口(18)窗口 (3) window functions(二)ProcessWindowFunction

ProcessWindowFunction   一些业务场景,我们需要收集窗口内所有的数据进行计算,例如计算窗口数据的中位数,或者计算窗口数据中出现频率最高的值。这样的需求,使用ReduceFunction和AggregateFunction就无法实现了。这个时候就需要ProcessWindowFunction了。 先来看接口定义 public abstr...