日期:2014-05-16  浏览次数:20469 次

Java 读写json格式的文件方法详解

一、要解决这个问题首先要知道json格式是什么?

JSON格式:
比如学生有学号,姓名,性别等。
用json表示则为:
{"studno":"11111","studname":"wwww","studsex":"男"}(各个字段都是字符型)

这代表一个学生的信息。

如果多个呢?

[{"studno":"122222","studname":"wwww","studsex":"男"},
{"studno":"11111","studname":"xxxx","studsex":"男"},
{"studno":"33333","studname":"ssss","studsex":"男"}]

这就是json格式。

二、那如何操作json格式的文件呢?

这个更简单了,说白了就是直接读写文件,再把读出来的文件内容格式化成json就可以了。

三、具体操作。

1.我有一个实体类,如下:

public class ElectSet {
public String xueqi;
public String xuenian;
public String startTime;
public String endTime;
public int menshu;
public String isReadDB;
//{"xueqi":,"xuenian":,"startTime":,"endTime":,"renshu":,"isReadDB":}
public String getXueqi() {
?? return xueqi;
}
public void setXueqi(String xueqi) {
?? this.xueqi = xueqi;
}
public String getXuenian() {
?? return xuenian;
}
public void setXuenian(String xuenian) {
?? this.xuenian = xuenian;
}
public String getStartTime() {
?? return startTime;
}
public void setStartTime(String startTime) {
?? this.startTime = startTime;
}
public String getEndTime() {
?? return endTime;
}
public void setEndTime(String endTime) {
?? this.endTime = endTime;
}
public int getMenshu() {
?? return menshu;
}
public void setMenshu(int menshu) {
?? this.menshu = menshu;
}
public String getIsReadDB() {
?? return isReadDB;
}
public void setIsReadDB(String isReadDB) {
?? this.isReadDB = isReadDB;
}

}

2.有一个json格式的文件,存的就是他的信息,如下

Sets.json:
{"xuenian":"2007-2008","xueqi":"1","startTime":"2009-07-19 08:30","endTime":"2009-07-22 18:00","menshu":"10","isReadDB":"Y"}

3.具体操作.
/*
* 取出文件内容,填充对象
*/
public ElectSet findElectSet(String path){
?? ElectSet electset=new ElectSet();
?? String sets=ReadFile(path);//获得json文件的内容
?? JSONObject jo=JSONObject.fromObject(sets);//格式化成json对象
?? //System.out.println("------------" jo);
?? //String name = jo.getString("xuenian");
?? //System.out.println(name);
?? electset.setXueqi(jo.getString("xueqi"));
?? electset.setXuenian(jo.getString("xuenian"));
?? electset.setStartTime(jo.getString("startTime"));
?? electset.setEndTime(jo.getString("endTime"));
?? electset.setMenshu(jo.getInt("menshu"));
?? electset.setIsReadDB(jo.getString("isReadDB"));
?? return electset;
}
//设置属性,并保存
public boolean setElect(String path,String sets){
?? try {
??? writeFile(path,sets);
??? return true;
?? } catch (IOException e) {
??? // TODO Auto-generated catch block
??? e.printStackTrace();
??? return false;
?? }
}
//读文件,返回字符串
public String ReadFile(String path){
??? File file = new File(path);
??? BufferedReader reader = null;
??? String laststr = "";
??? try {
???? //System.out.println("以行为单位读取文件内容,一次读一整行:");
???? reader = new BufferedReader(new FileReader(file));
???? String tempString = null;
???? int line = 1;
???? //一次读入一行,直到读入null为文件结束
???? while ((tempString = reader.readLine()) != null) {
????? //显示行号
????? System.out.println("line " line ": " tempString);
????? laststr = laststr tempString;
????? line ;
???? }
???? reader.close();
??? } catch (IOException e) {
???? e.printStackTrace();
??? } finally {
???? if (reader != null) {
????? try {
?????? reader.close();
????? } catch (IOException e1) {
????? }
???? }
??? }
??? return laststr;
}
???????? //把json格式的字符串写到文件
public void writeFile(String filePath, String sets) throws IOException {
??? FileWriter fw = new FileWriter(filePath);
??? PrintWriter out = new PrintWriter(fw);
??? out.write(sets);
??? out.pri