java解析在线json文本 package com.dxwl.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.net.URL;import java.nio.charset.Charset;
package com.dxwl.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONPath; import com.jfinal.kit.PropKit; public class ReadJsonFromUrl { private static Connection conn = null; public static void main(String[] args) throws Exception { long time1 = System.currentTimeMillis(); getConn(); ///////////////////////////////////// JSONObject json = readJsonFromUrl("http://haoyunhuo.cn/js/queryAllProvincesEx.js"); int provinceSize = (Integer)JSONPath.eval(json,"provinces.size()"); PreparedStatement pstProvince = null; String sqlProvince = "insert into sys_province(id,provinceName,pinYin,pinYinChar,indexId) " + "values(?,?,?,?,?)"; pstProvince = conn.prepareStatement(sqlProvince); for (int i = 0 ; i < provinceSize ; i++){ pstProvince.setString(1, JSONPath.eval(json,"provinces["+i+"].id").toString()); pstProvince.setString(2, JSONPath.eval(json,"provinces["+i+"].provinceName").toString()); pstProvince.setString(3, JSONPath.eval(json,"provinces["+i+"].pinYin").toString()); pstProvince.setString(4, JSONPath.eval(json,"provinces["+i+"].pinYinChar").toString()); pstProvince.setString(5, JSONPath.eval(json,"provinces["+i+"].indexId").toString()); pstProvince.execute(); } ///////////////////////////////////// json = readJsonFromUrl("http://haoyunhuo.cn/js/queryCities.js"); int citySize = (Integer)JSONPath.eval(json,"cities.size()"); PreparedStatement pstCity = null; String sqlCity = "insert into sys_city(id,name,cityPinyin,provinceId,hotCity,cityShortPY) " + "values(?,?,?,?,?,?)"; pstCity = conn.prepareStatement(sqlCity); for (int i = 0 ; i < citySize ; i++){ pstCity.setString(1, JSONPath.eval(json,"cities["+i+"].id").toString()); pstCity.setString(2, JSONPath.eval(json,"cities["+i+"].name").toString()); pstCity.setString(3, JSONPath.eval(json,"cities["+i+"].cityPinyin").toString()); pstCity.setString(4, JSONPath.eval(json,"cities["+i+"].provinceId").toString()); pstCity.setString(5, JSONPath.eval(json,"cities["+i+"].hotCity").toString()); pstCity.setString(6, JSONPath.eval(json,"cities["+i+"].cityShortPY").toString()); pstCity.execute(); } ///////////////////////////////////// json = readJsonFromUrl("http://haoyunhuo.cn/js/queryAllAreas.js"); int areaSize = (Integer)JSONPath.eval(json,"areas.size()"); PreparedStatement pst = null; String sqlArea = "insert into sys_area(id,cityId,cityName,areaName,provinceId,pinYin,pinYinChar,isShowWithCity) " + "values(?,?,?,?,?,?,?,?)"; pst = conn.prepareStatement(sqlArea); for (int i = 0 ; i < areaSize ; i++){ pst.setString(1, JSONPath.eval(json,"areas["+i+"].id").toString()); pst.setString(2, JSONPath.eval(json,"areas["+i+"].cityId").toString()); pst.setString(3, JSONPath.eval(json,"areas["+i+"].cityName").toString()); pst.setString(4, JSONPath.eval(json,"areas["+i+"].areaName").toString()); pst.setString(5, JSONPath.eval(json,"areas["+i+"].provinceId").toString()); pst.setString(6, JSONPath.eval(json,"areas["+i+"].pinYin").toString()); pst.setString(7, JSONPath.eval(json,"areas["+i+"].pinYinChar").toString()); pst.setString(8, JSONPath.eval(json,"areas["+i+"].isShowWithCity").toString()); pst.execute(); } long time2 = System.currentTimeMillis(); System.out.println(time2 - time1); } private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = JSONObject.parseObject(jsonText); return json; } finally { is.close(); } } public static void getConn() throws Exception { PropKit.use("db_config.properties"); conn = DriverManager.getConnection(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim()); } }