很多时候我们需要对Json文件进行读取,用来保存数据与对象的重新生成。
接上节内容,我将读取的是以下类的数据:
static class Obj {
private int x;
private int y;
private LinkedList interObjList;
Obj(int x, int y, LinkedList list) {
this.x = x;
this.y = y;
this.interObjList = list;
}
static class interObj {
private int a;
private int b;
public interObj(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "interObj{" +
"a='" + a +
", b=" + b +
'}';
}
}
LinkedList getInterObjList()
{
return interObjList;
}
void setX(int x) {
this.x = x;
}
void setY(int y) {
this.y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
@Override
public String toString() {
return "Obj{" +
"x='" + x +
", y=" + y +
'}';
}
}
生成json数据:
List objList = new LinkedList<>();
for(int i = 0; i < 10; ++i) {
LinkedList list = new LinkedList<>();
for(int j = 0; j < i + 1; ++j) {
list.add(new Obj.interObj(j, j + 1));
}
objList.add(new Obj(i, 2*i, list));
}
生成数据如下:
[{"x":0,"y":0,"interObjList":[{"a":0,"b":1}]},{"x":1,"y":2,"interObjList":[{"a":0,"b":1},{"a":1,"b":2}]},{"x":2,"y":4,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3}]},{"x":3,"y":6,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4}]},{"x":4,"y":8,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5}]},{"x":5,"y":10,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5},{"a":5,"b":6}]},{"x":6,"y":12,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5},{"a":5,"b":6},{"a":6,"b":7}]},{"x":7,"y":14,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5},{"a":5,"b":6},{"a":6,"b":7},{"a":7,"b":8}]},{"x":8,"y":16,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5},{"a":5,"b":6},{"a":6,"b":7},{"a":7,"b":8},{"a":8,"b":9}]},{"x":9,"y":18,"interObjList":[{"a":0,"b":1},{"a":1,"b":2},{"a":2,"b":3},{"a":3,"b":4},{"a":4,"b":5},{"a":5,"b":6},{"a":6,"b":7},{"a":7,"b":8},{"a":8,"b":9},{"a":9,"b":10}]}]
写入文件:
Gson gson = new Gson();
String jsonData = gson.toJson(objList);
try {
FileWriter writer = new FileWriter("/home/sar/Desktop/data.json");
writer.write(jsonData);
writer.flush();
writer.close();
System.out.println(jsonData);
}
catch (Exception ex) {
ex.printStackTrace();
}
读取文件:
BufferedReader bufferedReader = new BufferedReader(new FileReader("/home/sar/Desktop/data.json"));
Type listType = new TypeToken>(){}.getType();
LinkedList objs = gson.fromJson(bufferedReader, listType);
for(Obj o : objs) {
LinkedList.interObj> interObjs = o.getInterObjList();
for(Obj.interObj interObj : interObjs) {
System.out.println(interObj);
}
}
bufferedReader.close();
以下是读取结果:(即,将所有Obj中的interObjList打印出来)
interObj{a='0, b=1}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='5, b=6}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='5, b=6}
interObj{a='6, b=7}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='5, b=6}
interObj{a='6, b=7}
interObj{a='7, b=8}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='5, b=6}
interObj{a='6, b=7}
interObj{a='7, b=8}
interObj{a='8, b=9}
interObj{a='0, b=1}
interObj{a='1, b=2}
interObj{a='2, b=3}
interObj{a='3, b=4}
interObj{a='4, b=5}
interObj{a='5, b=6}
interObj{a='6, b=7}
interObj{a='7, b=8}
interObj{a='8, b=9}
interObj{a='9, b=10}