获得数据库的数据加入到JSONArray,再将数据填入到File中

来源:互联网 发布:java 日期格式化 星期 编辑:程序博客网 时间:2024/06/06 03:39

将数据库获取的数据转为json ,再加入JSONArray中,用   jsonArray.put(json);

//获取数据库的信息,转为json格式

JSONArray jsonArray = new JSONArray();String jsons = "";JSONObject json = new JSONObject();List<CarImageUser> CarImageUsers = new ArrayList<CarImageUser>();CarImageUser c = new CarImageUser();InterCarSomething ic = new CarSomething();List<Carinfo> cif = ic.GetCatInfo();List<CarImageUser> users = ic.User();for(int i=0;i<cif.size();i++){c.setLocationQ(users.get(i).getLocationQ());c.setLocationZ(users.get(i).getLocationZ());c.setUserName(users.get(i).getUserName());c.setDate(users.get(i).getDate());c.setTime(users.get(i).getTime());c.setCarNoYes(users.get(i).isCarNoYes());c.setCityNoYes(users.get(i).isCityNoYes());c.setImageViewName(users.get(i).getImageViewName());c.setImageViewPath(users.get(i).getImageViewPath());CarImageUsers.add(c);json = new JSONObject(c);jsonArray.put(json);}

//将String写入文件中try 

try {String jsonString = jsonArray.toString();System.out.print(FileName);File filejson = new File(Path.Path.FilePath+FileName+".json");if(!filejson.exists()){filejson.createNewFile();}FileWriter filewriter = new FileWriter(filejson.getAbsoluteFile());BufferedWriter bufferWriter = new BufferedWriter(filewriter);bufferWriter.write(jsonString);bufferWriter.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}


     


阅读全文
0 0
原创粉丝点击