Java读取json数据并存⼊数据库的操作代码Java读取json数据并存⼊数据库
1. pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2.students.json⽂件
{
"students": [
{
"stuId": 1,
"stuName": "meilanfang",
"stuAge": 93
},
{
"stuId": 2,
"stuName": "zhangguorong",
"stuAge": 92
},
{
"stuId": 3,
"stuName": "huangjiaju",
"stuAge": 91
}
]
}
3.读取json⽂件⽅式⼀
//读取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 = ad()) != -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 = ClassLoader().getResource("students.json").getPath();
String s = readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
JSONArray student = JSONArray("students");//构建JSONArray数组
for (int i = 0 ; i < student.size();i++){
JSONObject key = ((i);
int stuId= (("stuId");
String stuName= (("stuName");
int stuAge= (("stuAge");
#TODO 数据库操作
System.out.println(stuId);
System.out.println(stuName);
System.out.println(stuAge);
}
}
4.java 通过url下载图⽚保存到本地
/
/java 通过url下载图⽚保存到本地
public static void download(String urlString, int i) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输⼊流
InputStream is = InputStream();
// 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.获取聚合数据车辆服务
@Test
public void doGetTestOne() {
// 获得Http客户端(可以理解为:你得先有⼀个浏览器;注意:实际上HttpClient与浏览器是不⼀样的)
CloseableHttpClient httpClient = ate().build();
// 创建Get请求
CloseableHttpResponse response = null;
try {
/
/ 响应模型
for(int f=200;f<=300;f++){
HttpGet httpGet = new HttpGet("apis.juhe/cxdq/series?brandid=" + f + "&levelid=&key=XXXXXXXXXXXX"); // 由客户端执⾏(发送)Get请求
response = ute(httpGet);
// 从响应模型中获取响应实体
HttpEntity responseEntity = Entity();
System.out.println("响应状态为:" + StatusLine());
if (responseEntity != null) {
System.out.println("响应内容长度为:" + ContentLength());
// System.out.println("响应内容为:" + String(responseEntity));
JSONObject object = JSONObject.String(responseEntity));
JSONArray arr = JSONArray("result");
for (int i = 0; i < arr.size(); i++) {
JSONObject j = JSONObject(i);
CarBrandDetail vo = new CarBrandDetail();
vo.Integer("id"));
vo.String("name"));
vo.Integer("brandid"));
vo.Integer("levelid"));
vo.String("levelname"));
vo.String("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⽂件把数据存⼊数据库中
//读取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 = ad()) != -1) {
sb.append((char) ch);
}
fileReader.close();
getsavefilenamereader.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 = JSONObject("data").getJSONObject("map_set").getJSONObject("map_code_set")
.getJSONObject("mapSet").getJSONArray("markers");//构建JSONArray数组
// JSONArray movies = JSONObject("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) (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");
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存⼊数据库内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论