当前位置: 首页 > article >正文

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

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格式

{
	"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]
			}
		}
	]
}

http://www.kler.cn/a/385191.html

相关文章:

  • C++设计模式结构型模式———组合模式
  • 【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
  • 汽车广告常见特效处理有哪些?
  • 【p2p、分布式,区块链笔记 Torrent】WebTorrent 的lt_donthave插件
  • LeetCode:3255. 长度为 K 的子数组的能量值 II(模拟 Java)
  • 二分查找习题篇(上)
  • 音频内容理解
  • 爱奇艺大数据多AZ统一调度架构:打破数据孤岛,提升效率
  • 【系统架构设计师】高分论文:论软件的可用性设计
  • 如何快速搭建一个spring boot项目
  • FIPS203 后量子安全ML-KEM(标准简读)
  • .vue文件中定义变量和在引用的.ts文件中定义变量的区别
  • C++模拟真人动态生成鼠标滑动路径
  • 29种Prompt Engineering
  • 自监督学习:机器学习的未来新方向
  • Docker篇(阿里云私服)
  • 热成像手机VS传统热成像仪:AORO A23为何更胜一筹?
  • 64 mysql 的 表锁
  • 建筑安全员题库分享
  • SpringBoot启动器
  • 性能调优专题(5)之深入理解Mysql事务隔离级别与锁机制
  • 4.WebSocket 配置与Nginx 的完美结合
  • 低代码与数字化综合服务平台的建设与探索
  • 淘宝反爬虫机制的主要手段有哪些?
  • 【计算机网络篇】数据链路层(14)虚拟局域网VLAN(概述,实现机制)
  • SpringBoot源码解析(二)