so use gson stream
read
File jsonInputFile = new File(path);
FileInputStream fisInput;
JsonReader reader;
try {
fisInput = new FileInputStream(jsonInputFile);
reader = new JsonReader(new InputStreamReader(fisInput, "UTF-8"));
ArrayList<Map> categoryALHM = gson.fromJson(reader,
ArrayList.class);
if(null == categoryALHM || categoryALHM.size() == 0){
log.info(path+":is null or empty");
return false;
}
reader.close();
fisInput.close();
} catch (Exception e) {
log.info(e.getMessage().toString());
return false;
}
write
File jsonFile = new File(originalDatpath);
FileOutputStream fos;
JsonWriter writer;
try {
fos = new FileOutputStream(jsonFile);
writer = new JsonWriter(new OutputStreamWriter(fos, "UTF-8"));
writer.setIndent(" ");
Map getCategoryMap = JsonTool.Json2Map(IbobarDataUtil
.viewCategory());
ArrayList<HashMap<String, Object>> getCategoryMapList = (ArrayList) getCategoryMap
.get("list");
log.info("original category:" + getCategoryMapList);
gson.toJson(getCategoryMapList, ArrayList.class, writer);
writer.close();
fos.flush();
fos.close();
} catch (Exception e) {
log.info(e.getMessage().toString());
}
read and write
File jsonInputFile = new File(organizeDataPath);
File jsonOutputFile = new File(usingDatPath);
FileOutputStream fosOutput;
FileInputStream fisInput;
JsonWriter writer;
JsonReader reader;
try {
fosOutput = new FileOutputStream(jsonOutputFile);
writer = new JsonWriter(new OutputStreamWriter(fosOutput, "UTF-8"));
writer.setIndent(" ");
fisInput = new FileInputStream(jsonInputFile);
reader = new JsonReader(new InputStreamReader(fisInput, "UTF-8"));
writer.beginObject();
reader.beginObject();
while (reader.hasNext()) {
String key = reader.nextName();
List<Map> bookOranizeList = gson.fromJson(reader,
ArrayList.class);
writer.name(key);
gson.toJson(bookOranizeList, ArrayList.class, writer);
}
writer.endObject();
writer.close();
fosOutput.flush();
fosOutput.close();
reader.endObject();
reader.close();
fisInput.close();
} catch (Exception e) {
log.info(e.getMessage().toString());
}
沒有留言:
張貼留言