json格式数据转sql语句
Posted hanruikai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json格式数据转sql语句相关的知识,希望对你有一定的参考价值。
package com.puhui.flowplatform.entry;
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;
/**
*/
public class Test
private static final String PATH = "/Users/hanruikai/city.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();
JsonArray jsonArray = (JsonArray) jsonParser.parse(new FileReader(PATH));
for (int i = 0; i < jsonArray.size(); i++)
JsonElement jsonElement = jsonArray.get(i);
JsonObject featuresObj = jsonElement.getAsJsonObject();
String name = featuresObj.get("name").toString();
String pinyin = featuresObj.get("pinyin").toString();
String sqlStr = "insert into credit_card_city_info (id, city_name, initial,state,ishot,online_time,down_time,create_time,update_time,remark) values (null,"+name+","+pinyin+",null,null,null,null,null,null,null); \\r\\n";
System.out.println(sqlStr);
File file = new File("/Users/hanruikai/city.sql");
if (!file.exists())
file.createNewFile();
FileWriter fileWriter = new FileWriter(file, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(sqlStr);
bufferedWriter.close();
以上是关于json格式数据转sql语句的主要内容,如果未能解决你的问题,请参考以下文章