Code Monkey home page Code Monkey logo

Comments (1)

liyujiang-gzu avatar liyujiang-gzu commented on June 27, 2024

这个,你自己写一个Java程序操作city.json,就可以把单独省的数据提取出来变成city2.json呢。下面这个我是从某个项目提供的贵州省的SQLite数据库数据中转换得来的,参考哈:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        Statement stat = conn.createStatement();
        saveToJsonString("test.json", queryProvince(stat));
        for (int i = 1; i < 11; i++) {
            ArrayList<County> counties = queryCounty(stat, i);
            saveToJsonString("city" + i + ".json", counties);
        }
        stat.close();
        conn.close();
    }

    private static void saveToJsonString(String name, Object object) {
        String jsonStr = JSON.toJSONString(object, SerializerFeature.PrettyFormat);
        System.out.println(jsonStr);
        try {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(name), "utf-8"));
            writer.write(jsonStr);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static ArrayList<Province> queryProvince(Statement stat) throws SQLException {
        ArrayList<Province> provinces = new ArrayList<Province>();
        ResultSet provinceRs = stat.executeQuery("select id,name,sign_key from province");
        while (provinceRs.next()) {
            Province province = new Province();
            province.value = provinceRs.getString("sign_key");
            province.text = provinceRs.getString("name").trim();
            int id = provinceRs.getInt("id");
            ArrayList<City> cities = queryCity(stat, id);
            saveToJsonString("province" + id + ".json", cities);
            province.children.addAll(cities);
            provinces.add(province);
        }
        provinceRs.close();
        return provinces;
    }

    private static ArrayList<City> queryCity(Statement stat, int provinceId) throws SQLException {
        ArrayList<City> cities = new ArrayList<City>();
        ResultSet cityRs = stat.executeQuery("select id,name,sign_key from city where parent_key=" + provinceId);
        while (cityRs.next()) {
            City city = new City();
            city.value = cityRs.getString("sign_key");
            city.text = cityRs.getString("name").trim();
//            int id = cityRs.getInt("id");
//            ArrayList<County> counties = queryCounty(stat, id);
//            saveToJsonString("city" + id + ".json", cities);
//            city.children.addAll(counties);
            cities.add(city);
        }
        cityRs.close();
        return cities;
    }

    private static ArrayList<County> queryCounty(Statement stat, int cityId) throws SQLException {
        ArrayList<County> counties = new ArrayList<County>();
        ResultSet countyRs = stat.executeQuery("select id,name,sign_key from county where parent_key=" + cityId);
        while (countyRs.next()) {
            County county = new County();
            county.value = countyRs.getString("sign_key");
            county.text = countyRs.getString("name").trim();
            counties.add(county);
        }
        countyRs.close();
        return counties;
    }

}

from androidpicker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.