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();
        }
    }
}

你可能感兴趣的:(java技术,互联网技术栈)