"json学习6——>Java读写json格式的文件方法详解": json学习六——>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.println();
fw.close();
out.close();
}
4.调用,使用(在网站的controller里调用的)
//取出json对象
public void getelectsettings(httpservletrequest request,
httpservletresponse response) throws exception {
electset electset=new electset();
string abspath = request.getrealpath("\\");
string filepath = abspath "public\\sets\\electsets.json";
electset=businessservice.findelectsets(filepath);//这里是调用,大家自己改改,我调用的业务层 的。
jsonarray jsonitems = new jsonarray();
jsonitems.add(electset);
jsonobject jo=new jsonobject();
jo.put("data", jsonitems);
system.out.println(jo);
request.setcharacterencoding("utf-8");
response.setcontenttype("application/json;charset=utf-8");
printwriter out = response.getwriter();
out.print(jo);
}
//修改json文件
public void changeelectset(httpservletrequest request,
httpservletresponse response) throws exception {
request.setcharacterencoding("utf-8");
response.setcontenttype("text/json;charset=utf-8");
log.info("reach changeelectset");
string json = (string) request.getparameter("json").trim();
log.info("change electset");
log.info(json);
electset sets = new electset();
jsonobject jsonobject = jsonobject.fromobject(json);
sets = (electset) jsonobject.tobean(jsonobject, electset.class);
if(sets.getisreaddb()=="false"){
sets.setisreaddb("否");
}
else{
sets.setisreaddb("是");
}
string changes="{\"xuenian\":\"";//因为json的属性要用引号,所以要用\"转义一下
changes =sets.getxuenian() "\",\"xueqi\":\"" sets.getxueqi() "\",\"starttime\":\"" sets.getstarttime() "\",\"endtime\":\"" sets.getendtime() "\",\"menshu\":\"" sets.getmenshu() "\",\"isreaddb\":\"" sets.getisreaddb() "\"}";
system.out.println(changes);
string abspath = request.getrealpath("\\");
string filepath = abspath "public\\sets\\electsets.json";
if(businessservice.changeelectsets(filepath, changes)){
log.info("选课设置修改成功");
request.setcharacterencoding("utf-8");
response.setcontenttype("text/json;charset=utf-8");
response.getwriter().write("{success: true}");
} else {
response.getwriter().write("{errors:'选课设置修改失败,请重新尝试!'}");
}
}
摘自红色黑客联盟() 原文:
搜索此文相关文章: