- pom文件
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160810</version> </dependency> <!--mysql 依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
- application.properties文件
#本机调试 spring.datasource.url = jdbc:mysql://localhost:3306/baidudata?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #端口号 server.port=18082 #指明mybatis实体类包位置 mybatis.type-aliases-package= bababi.springboot.demo.model #指明mappers位置,可以指明多个位置 mybatis.mapper-locations=classpath:map/*.xml mybatis.configuration.call-setters-on-nulls=true
- GetJson文件
public class GetJson { public JSONObject getHttpJson(String url, int comefrom) throws Exception { try { URL realUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); //请求成功 if (connection.getResponseCode() == 200) { InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //10MB的缓存 byte[] buffer = new byte[10485760]; int len = 0; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } String jsonString = baos.toString(); baos.close(); is.close(); //转换成json数据处理 // getHttpJson函数的后面的参数1,表示返回的是json数据,2表示http接口的数据在一个()中的数据 JSONObject jsonArray = getJsonString(jsonString, comefrom); return jsonArray; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return null; } public JSONObject getJsonString(String str, int comefrom) throws Exception{ JSONObject jo = null; if(comefrom==1){ return new JSONObject(str); }else if(comefrom==2){ int indexStart = 0; //字符处理 for(int i=0;i<str.length();i++){ if(str.charAt(i)=='('){ indexStart = i; break; } } String strNew = ""; //分割字符串 for(int i=indexStart+1;i<str.length()-1;i++){ strNew += str.charAt(i); } return new JSONObject(strNew); } return jo; } }
4.测试类
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Autowired private JokeMapper jokeMapper; @Test public void contextLoads() { String address="这里写你要请求的接口路径如www.baidu.com/getdata"; try { JSONObject dayLine = new GetJson().getHttpJson(address,1); System.out.println(dayLine); //取得data的json数据 JSONArray json= null; json = dayLine.getJSONArray("joke"); List<Joke> list= JSON.parseArray(json.toString(),Joke.class); System.out.println(list); for (Joke btcoinEntity:list){ //遍历list jokeMapper.insert(btcoinEntity); //插入数据库 } } catch (Exception e) { e.printStackTrace(); } }
5.这里还有dao层接口JokeMapper、以及JokeMapper.xml、Joke(实体类,需要跟你爬取数据字一一对应)没写,可以通过逆向工程生成,最后运行test 数据成功插入数据库。 ------------参考文章https://blog.csdn.net/rui15111/article/details/80974172
- GetJson文件
转载于:https://my.oschina.net/u/3836038/blog/2876872