解析json导出csv或者直接入库

java代码

java 复制代码
package org.example.demo.demos.web;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JsonToCsvExporter {

    public static String readJsonFromFile(String filePath) throws IOException {
        return new String(Files.readAllBytes(Paths.get(filePath)));
    }
    public static void main(String[] args) {
        try {
            String jsonString = readJsonFromFile("D:\\wxfile\\WeChat Files\\wxid_apkc5xaef63v22\\FileStorage\\File\\2024-11\\320506.json");
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray features = jsonObject.getJSONArray("features");

            //BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\sql\\jiayou.csv"));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\sql\\jiayou6.csv"), "UTF-8"));

            // Write CSV header
            writer.write("province,city,country,name,code,centroid_lat,centroid_lon");
            writer.newLine();

            for (int i = 0; i < features.length(); i++) {
                JSONObject feature = features.getJSONObject(i);
                JSONObject properties = feature.getJSONObject("properties");
                JSONArray centroid = properties.getJSONArray("centroid");

                String province = properties.getString("province");
                String city = properties.getString("city");
                String country = properties.getString("country");
                String name = properties.getString("name");
                String code = properties.getString("code");
                // 使用BigDecimal处理精度
                Double centroidLat = centroid.getDouble(1);
                Double centroidLon = centroid.getDouble(0);
                JSONArray coordinates = feature.getJSONObject("geometry").getJSONArray("coordinates");



                // 使用引号包裹数值
                writer.write(String.format("%s,%s,%s,%s,%s,\"%s\",\"%s\",\"%s\"",
                        province, city, country, name, code, centroidLat+"\t", centroidLon, coordinates.toString()));
                writer.newLine();
            }

            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

json格式

json 复制代码
{
	"type": "FeatureCollection",
	"features": [{
			"type": "Feature",
			"geometry": {
				"type": "Polygon",
				"coordinates": [
					[
						[120.60007823970665, 31.31712313141799],
						[120.60079346065866, 31.319310309073238],
						[120.60063063950828, 31.320359162324976],
						[120.60007823970665, 31.31712313141799]
					]
				]
			},
			"properties": {
				"geom": "SRID=4326;M6028 31.314280)))",
				"province": "江苏省",
				"city": "苏州市",
				"country": "姑苏区",
				"name": "虎丘街道",
				"code": "320508022",
				"centroid": [120.58022070333683, 31.333360742651255]
			}
		},
		
		{
			"type": "Feature",
			"geometry": {
				"type": "Polygon",
				"coordinates": [
					[
						[120.64872047263296, 31.28158308108988],
						[120.64836632087228, 31.280224143708896],
						[120.64872047263296, 31.28158308108988]
					]
				]
			},
			"properties": {
				"geom": "SRID=4326;MULT1.27843)))",
				"province": "江苏省",
				"city": "苏州市",
				"country": "姑苏区",
				"name": "葑门街道",
				"code": "320508",
				"centroid": [120.64269225845081, 31.29180955836013]
			}
		}
	]
}
相关推荐
DevOpsDojo2 分钟前
HTML语言的数据结构
开发语言·后端·golang
懒大王爱吃狼4 分钟前
Python绘制数据地图-MovingPandas
开发语言·python·信息可视化·python基础·python学习
数据小小爬虫7 分钟前
如何使用Python爬虫按关键字搜索AliExpress商品:代码示例与实践指南
开发语言·爬虫·python
好一点,更好一点23 分钟前
systemC示例
开发语言·c++·算法
不爱学英文的码字机器26 分钟前
[操作系统] 环境变量详解
开发语言·javascript·ecmascript
martian66530 分钟前
第17篇:python进阶:详解数据分析与处理
开发语言·python
无码不欢的我34 分钟前
使用vscode在本地和远程服务器端运行和调试Python程序的方法总结
ide·vscode·python
五味香35 分钟前
Java学习,查找List最大最小值
android·java·开发语言·python·学习·golang·kotlin
时韵瑶40 分钟前
Scala语言的云计算
开发语言·后端·golang
金融OG41 分钟前
99.8 金融难点通俗解释:净资产收益率(ROE)
大数据·python·线性代数·机器学习·数学建模·金融·矩阵