解析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]
			}
		}
	]
}
相关推荐
Ajiang282473530426 分钟前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
幽兰的天空31 分钟前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10223 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou4 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
安静读书4 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频
----云烟----5 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024066 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it6 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎