解析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]
			}
		}
	]
}
相关推荐
觅远2 分钟前
python实现word转html
python·html·word
froginwe1126 分钟前
PostgreSQL表达式的类型
开发语言
悠然的笔记本28 分钟前
python2和python3的区别
python
委婉待续29 分钟前
java抽奖系统(八)
java·开发语言·状态模式
deja vu水中芭蕾31 分钟前
嵌入式C面试
c语言·开发语言
爱码小白31 分钟前
PyQt5 学习方法之悟道
开发语言·qt·学习方法
西猫雷婶1 小时前
python学opencv|读取图像(十六)修改HSV图像HSV值
开发语言·python·opencv
lovelin+v175030409661 小时前
智能电商:API接口如何驱动自动化与智能化转型
大数据·人工智能·爬虫·python
weixin_537590451 小时前
《Java编程入门官方教程》第八章练习答案
java·开发语言·servlet
lsx2024061 小时前
MVC 发布
开发语言