我是靠谱客的博主 复杂未来,这篇文章主要介绍解析从Mysql导出的JSON文件,现在分享给大家,希望可以做个参考。

思路:
  1、json文件解析成一个string。
  2、用fastjson直接解析即可。


原始数据mysql:


导出的json文件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{ "RECORDS":[ { "id":1201, "name":"gopal", "deg":"manager", "salary":50000, "dept":"TP" }, { "id":1202, "name":"manisha", "deg":"Proof reader", "salary":50000, "dept":"TP" },...

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
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class JSONFileToString { public String ReadFile(String Path){ BufferedReader reader = null; String laststr = ""; try{ FileInputStream fileInputStream = new FileInputStream(Path); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String tempString = null; while((tempString = reader.readLine()) != null){ laststr += tempString; } reader.close(); }catch(IOException e){ e.printStackTrace(); }finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return laststr; } }

复制代码
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
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.io.FileNotFoundException; public class ParseMysqlJsonFile { public static void main(String[] args) throws FileNotFoundException { String jsonFile = "C:\Users\lenovo\Desktop\emp.json"; //输入JSON文件=> String jsonContext = new JSONFileToString().ReadFile(jsonFile); //解析json对象 JSONObject jsonObject = JSON.parseObject(jsonContext); //得到json数组 JSONArray records = jsonObject.getJSONArray("RECORDS"); //得到标准json for (Object record : records) { System.out.println(record.toString()); /**结果**/ //{"deg":"manager","name":"gopal","id":1201,"dept":"TP","salary":50000} //{"deg":"Proof reader","name":"manisha","id":1202,"dept":"TP","salary":50000} //{"deg":"php dev","name":"khalil","id":1203,"dept":"AC","salary":30000} //{"deg":"php dev","name":"prasanth","id":1204,"dept":"AC","salary":30000} //{"deg":"admin","name":"kranthi","id":1205,"dept":"TP","salary":20000} //{"deg":"grp des","name":"satish p","id":1206,"salary":20000} } } }

最后

以上就是复杂未来最近收集整理的关于解析从Mysql导出的JSON文件的全部内容,更多相关解析从Mysql导出内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部