概述
思路:
1、json文件解析成一个string。
2、用fastjson直接解析即可。
原始数据mysql:
导出的json文件:
{ "RECORDS":[ { "id":1201, "name":"gopal", "deg":"manager", "salary":50000, "dept":"TP" }, { "id":1202, "name":"manisha", "deg":"Proof reader", "salary":50000, "dept":"TP" },...
json文件转成字符串
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; } }
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导出的JSON文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复