Java reads json data and stores it into the database
1. pom dependency
<dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
document
{ "students": [ { "stuId": 1, "stuName": "meilanfang", "stuAge": 93 }, { "stuId": 2, "stuName": "zhangguorong", "stuAge": 92 }, { "stuId": 3, "stuName": "huangjiaju", "stuAge": 91 } ] }
3. Read json file method one
//Read the json file public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = ()) != -1) { ((char) ch); } (); (); jsonStr = (); return jsonStr; } catch (IOException e) { (); return null; } } public static void main(String[] args) { String path = ().getResource("").getPath(); String s = readJsonFile(path); JSONObject jobj = (s); JSONArray student = ("students");//Build JSONArray array for (int i = 0 ; i < ();i++){ JSONObject key = (JSONObject)(i); int stuId= (Integer)("stuId"); String stuName= (String)("stuName"); int stuAge= (Integer)("stuAge"); #TODO Database Operations (stuId); (stuName); (stuAge); } }
Download the image through url and save it to local
//Java download the image and save it to local public static void download(String urlString, int i) throws Exception { // Construct URL URL url = new URL(urlString); // Open the connection URLConnection con = (); // Input stream InputStream is = (); // 1K data buffering byte[] bs = new byte[1024]; // The length of the data read int len; // Output file stream String filename = "D:\\Picture Download/" + i + ".jpg"; //Download path and download image name File file = new File(filename); FileOutputStream os = new FileOutputStream(file, true); // Start reading while ((len = (bs)) != -1) { (bs, 0, len); } (i); // Complete, close all links (); (); }
5. Obtain aggregated data vehicle services
@Test public void doGetTestOne() { // Get the Http client (it can be understood as: you have to have a browser first; note: In fact, HttpClient is different from the browser) CloseableHttpClient httpClient = ().build(); // Create a Get request CloseableHttpResponse response = null; try { // Response model for(int f=200;f<=300;f++){ HttpGet httpGet = new HttpGet("/cxdq/series?brand&levelid=&key=XXXXXXXXXXXX"); // Execute (send) Get request by the client response = (httpGet); // Get the response entity from the response model HttpEntity responseEntity = (); ("The response status is:" + ()); if (responseEntity != null) { ("The response content length is:" + ()); // ("Response content is:" + (responseEntity)); JSONObject object = ((responseEntity)); JSONArray arr = ("result"); for (int i = 0; i < (); i++) { JSONObject j = (i); CarBrandDetail vo = new CarBrandDetail(); (("id")); (("name")); (("brandid")); (("levelid")); (("levelname")); (("sname")); (new Date()); int insert = (vo); if (insert > 0) { ("true"); } } } } }catch (ClientProtocolException e) { (); } catch (ParseException e) { (); } catch (IOException e) { (); } finally { try { // Free up resources if (httpClient != null) { (); } if (response != null) { (); } } catch (IOException e) { (); } } }
ps: Java reads json file and stores data into the database
//Read the json file public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = ()) != -1) { ((char) ch); } (); (); jsonStr = (); return jsonStr; } catch (IOException e) { (); return null; } } public Result<?> test() { String s = readJsonFile("D:\\"); JSONObject jobj = (s); JSONArray jsonArray = ("data").getJSONObject("map_set").getJSONObject("map_code_set") .getJSONObject("mapSet").getJSONArray("markers");//Build JSONArray array// JSONArray movies = ("data").getJSONObject("map_set"). // getJSONObject("map_code_set").getJSONObject("mapSet").getJSONArray("polyline");//Build JSONArray array for (int i = 0; i < (); i++) { LongMarchStation longMarchStation = new LongMarchStation(); JSONObject key = (JSONObject) (i); JSONObject jsonObject = ((JSONObject) (i)).getJSONObject("callout"); String id = ("id") + ""; String latitude = ("latitude") + ""; String longitude = ("longitude") + ""; Integer min = (Integer) ("min"); Integer max = (Integer) ("max"); String iconPath = (String) ("iconPath"); String name = (String) ("content"); (id); (latitude); (longitude); (max); (min); (name); (iconPath); (longMarchStation); } return ("Added successfully!"); }
This is the article about Java reading json data and storing it into the database. For more information about Java son storage in the database, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!