Java讀取json數據并存入數據庫
1. pom依賴
1
2
3
4
5
|
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 47 </version> </dependency> |
2.students.json文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
{ "students" : [ { "stuId" : 1 , "stuName" : "meilanfang" , "stuAge" : 93 }, { "stuId" : 2 , "stuName" : "zhangguorong" , "stuAge" : 92 }, { "stuId" : 3 , "stuName" : "huangjiaju" , "stuAge" : 91 } ] } |
3.讀取json文件方式一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
//讀取json文件 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 = reader.read()) != - 1 ) { sb.append(( char ) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null ; } } public static void main(String[] args) { String path = JsonTest. class .getClassLoader().getResource( "students.json" ).getPath(); String s = readJsonFile(path); JSONObject jobj = JSON.parseObject(s); JSONArray student = jobj.getJSONArray( "students" ); //構建JSONArray數組 for ( int i = 0 ; i < student.size();i++){ JSONObject key = (JSONObject)student.get(i); int stuId= (Integer)key.get( "stuId" ); String stuName= (String)key.get( "stuName" ); int stuAge= (Integer)key.get( "stuAge" ); #TODO 數據庫操作 System.out.println(stuId); System.out.println(stuName); System.out.println(stuAge); } } |
4.java 通過url下載圖片保存到本地
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
//java 通過url下載圖片保存到本地 public static void download(String urlString, int i) throws Exception { // 構造URL URL url = new URL(urlString); // 打開連接 URLConnection con = url.openConnection(); // 輸入流 InputStream is = con.getInputStream(); // 1K的數據緩沖 byte [] bs = new byte [ 1024 ]; // 讀取到的數據長度 int len; // 輸出的文件流 String filename = "D:\\圖片下載/" + i + ".jpg" ; //下載路徑及下載圖片名稱 File file = new File(filename); FileOutputStream os = new FileOutputStream(file, true ); // 開始讀取 while ((len = is.read(bs)) != - 1 ) { os.write(bs, 0 , len); } System.out.println(i); // 完畢,關閉所有鏈接 os.close(); is.close(); } |
5.獲取聚合數據車輛服務
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
@Test public void doGetTestOne() { // 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 創建Get請求 CloseableHttpResponse response = null ; try { // 響應模型 for ( int f= 200 ;f<= 300 ;f++){ HttpGet httpGet = new HttpGet( "http://apis.juhe.cn/cxdq/series?brandid=" + f + "&levelid=&key=XXXXXXXXXXXX" ); // 由客戶端執行(發送)Get請求 response = httpClient.execute(httpGet); // 從響應模型中獲取響應實體 HttpEntity responseEntity = response.getEntity(); System.out.println( "響應狀態為:" + response.getStatusLine()); if (responseEntity != null ) { System.out.println( "響應內容長度為:" + responseEntity.getContentLength()); // System.out.println("響應內容為:" + EntityUtils.toString(responseEntity)); JSONObject object = JSONObject.parseObject(EntityUtils.toString(responseEntity)); JSONArray arr = object.getJSONArray( "result" ); for ( int i = 0 ; i < arr.size(); i++) { JSONObject j = arr.getJSONObject(i); CarBrandDetail vo = new CarBrandDetail(); vo.setId(j.getInteger( "id" )); vo.setName(j.getString( "name" )); vo.setBrandId(j.getInteger( "brandid" )); vo.setLevelId(j.getInteger( "levelid" )); vo.setLevelName(j.getString( "levelname" )); vo.setSname(j.getString( "sname" )); vo.setCreateTime( new Date()); int insert = carBrandMapper.insert(vo); if (insert > 0 ) { System.out.println( "true" ); } } } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 釋放資源 if (httpClient != null ) { httpClient.close(); } if (response != null ) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } } |
ps:java讀取json文件把數據存入數據庫中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
//讀取json文件 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 = reader.read()) != - 1 ) { sb.append(( char ) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null ; } } public Result<?> test() { String s = readJsonFile( "D:\\marks.json" ); JSONObject jobj = JSON.parseObject(s); JSONArray jsonArray = jobj.getJSONObject( "data" ).getJSONObject( "map_set" ).getJSONObject( "map_code_set" ) .getJSONObject( "mapSet" ).getJSONArray( "markers" ); //構建JSONArray數組 // JSONArray movies = jobj.getJSONObject("data").getJSONObject("map_set"). // getJSONObject("map_code_set").getJSONObject("mapSet").getJSONArray("polyline");//構建JSONArray數組 for ( int i = 0 ; i < jsonArray.size(); i++) { LongMarchStation longMarchStation = new LongMarchStation(); JSONObject key = (JSONObject) jsonArray.get(i); JSONObject jsonObject = ((JSONObject) jsonArray.get(i)).getJSONObject( "callout" ); String id = key.get( "id" ) + "" ; String latitude = key.get( "latitude" ) + "" ; String longitude = key.get( "longitude" ) + "" ; Integer min = (Integer) key.get( "min" ); Integer max = (Integer) key.get( "max" ); String iconPath = (String) key.get( "iconPath" ); String name = (String) jsonObject.get( "content" ); longMarchStation.setId(id); longMarchStation.setLatitude(latitude); longMarchStation.setLongitude(longitude); longMarchStation.setMax(max); longMarchStation.setMin(min); longMarchStation.setName(name); longMarchStation.setIconPath(iconPath); longMarchStationService.save(longMarchStation); } return Result.ok( "添加成功!" ); } |
到此這篇關于Java讀取json數據并存入數據庫的文章就介紹到這了,更多相關JAVA son存入數據庫內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/hsadfdsahfdsgfds/article/details/112704702