本文目录一览:
- 1、java怎么读取json格式的数据
- 2、java如何读取json中文件内容
- 3、java类中怎么读取 json文件
- 4、java中如何读取json文件,在本地有E:/a.json文件,想读取这个json文件里面的内容,怎样实现
- 5、java读取json文件的问题
- 6、JAVA 如何读取JSON编码格式的数据?
java怎么读取json格式的数据
java可以使用JSONObject和JSONArray来操作json对象和json数组,具体用法如下
1:java对象与json串转换:
java对象—json串:
JSONObject JSONStr = JSONObject.fromObject(object);
String str = JSONStr.toString();
json串—java对象:
JSONObject jsonObject = JSONObject.fromObject( jsonString );
Object pojo = JSONObject.toBean(jsonObject,pojoCalss);
2:java数组对象与json串转换:
java数组—json串:
JSONArray arrayStr = JSONArray.fromObject(List?);
String str = arrayStr.toString();
json串—java数组:
JSONArray array = JSONArray.fromObject(str);
List? list = JSONArray.toList(array, ?.class);
java如何读取json中文件内容
java读取文件非常简单的
File file = new File("D:/java"); //给定一个目录
File[] list = file.listFiles(); //获取目录下的所有文件
for(int i=0; ilist.length; i++){
if(list[i].isFile()){ //判断是否为文件
InputStreamReader isr = new InputStreamReader(new FileInputStream(list[i]),"UTF-8");//读取文件,同时指定编码
StringBuffer sb = new StringBuffer();
char[] ch = new char[128]; //一次读取128个字符
int len = 0;
while((len = isr.read(ch,0, ch.length)) != -1){
sb.append(ch, 0, len);
}
isr.close();
System.out.println(sb); //将读取完的文件打印出来,你要怎么处理都可以了
}
}
java类中怎么读取 json文件
String fullFileName = "E:/a.json";
File file = new File(fullFileName);
Scanner scanner = null;
StringBuilder buffer = new StringBuilder();
try {
scanner = new Scanner(file, "utf-8");
while (scanner.hasNextLine()) {
buffer.append(scanner.nextLine());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} finally {
if (scanner != null) {
scanner.close();
}
}
System.out.println(buffer.toString());
这是读取文件的方法,至于解析json,则你自己弄吧
java中如何读取json文件,在本地有E:/a.json文件,想读取这个json文件里面的内容,怎样实现
//saveJsonFile("E:\\yindd\\slaughter.json");
//json文件存放路径(如:E:\a.json)
String data= ReadFile.readFile("F:\\a.json");
System.out.println(data);
JSONObject jsonObj = JSONObject.fromObject(data);
//得到A对象
JSONArray arrayA=jsonObj.getJSONArray("A");
A a = (A) JSONObject.toBean((JSONArray.fromObject(arrayA.toString()).getJSONObject(0)),A.class);
//得到B集合
JSONArray arrayB=jsonObj.getJSONArray("B");
ListB listB=new ArrayListB();
for(int i=0;iarrayB.size();i++){
B b=(B)JSONObject.toBean((JSONArray.fromObject(arrayB.toString()).getJSONObject(i)),B.class);
listB.add(b);
}
//得到C集合
JSONArray arrayC=jsonObj.getJSONArray("C");
ListC listC=new ArrayListC();
for(int i=0;iarrayB.size();i++){
C c=(C)JSONObject.toBean((JSONArray.fromObject(arrayC.toString()).getJSONObject(i)),C.class);
listB.add(c);
}
java读取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";
JAVA 如何读取JSON编码格式的数据?
java可以使用JSONObject和JSONArray来操作json对象和json数组,具体用法如下
1:java对象与json串转换:
java对象—json串:
JSONObject JSONStr = JSONObject.fromObject(object);
String str = JSONStr.toString();
json串—java对象:
JSONObject jsonObject = JSONObject.fromObject( jsonString );
Object pojo = JSONObject.toBean(jsonObject,pojoCalss);
2:java数组对象与json串转换:
java数组—json串:
JSONArray arrayStr = JSONArray.fromObject(List?);
String str = arrayStr.toString();
json串—java数组:
JSONArray array = JSONArray.fromObject(str);
List? list = JSONArray.toList(array, ?.class);