JSON数据转SQL数据

        public static void main(String[] args) throws Exception {
		jsonToSql("D:\\1.json");
	}

	private static void jsonToSql(String path) throws Exception {
		JsonParser jsonParser = new JsonParser();
		JsonObject jsonObject = (JsonObject) jsonParser.parse(new FileReader(path));
		JsonElement features = jsonObject.get("item_cat");
		JsonArray asJsonArray = features.getAsJsonArray();
		for (int i = 0; i < asJsonArray.size(); i++) {
			JsonElement jsonElement = asJsonArray.get(i);
			JsonObject featuresObj = jsonElement.getAsJsonObject();

			String cid = featuresObj.get("cid").toString();
			String is_parent = featuresObj.get("is_parent").toString();
			String name = featuresObj.get("name").toString();
			String parent_cid = featuresObj.get("parent_cid").toString();

			String sqlStr = "insert into TaoBao_Item (cid, is_parent, name, parent_cid) values (" + cid + ", " + is_parent + ", " + name + ", " + parent_cid + "); \r\n";
			System.out.println(sqlStr);
			File file = new File("D:\\2.sql");
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file, true);
			BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
			bufferedWriter.write(sqlStr);
			bufferedWriter.close();
		}
	}


同类文章