生成json文件和解析json文件

来源:互联网 发布:7天学会知乎 编辑:程序博客网 时间:2024/04/29 11:27

在开发中很多时候会遇到json文件的解析和把数据库中的数据写成json文件的形式存储起来。

代码如下:

//把数据库中的数据写成json格式的文件存储到SD卡中public static void backupsNote(SQLiteDatabase db) {JSONObject allData = new JSONObject(); // 建立最外面的JSONObjectJSONArray array = new JSONArray(); // 定义新的JSONArray对象Cursor cursor = db.rawQuery("select * from note ", null);while (cursor.moveToNext()) {if (cursor.getInt(0) > 0) {JSONObject temp = new JSONObject(); // 创建一个新的JSONObject对象try {temp.put(NoteColumns.ID, cursor.getInt(0));temp.put(NoteColumns.PARENT_ID, cursor.getInt(1));temp.put(NoteColumns.ALERTED_DATE, cursor.getLong(2));temp.put(NoteColumns.BG_COLOR_ID, cursor.getInt(3));temp.put(NoteColumns.CREATED_DATE, cursor.getLong(4));temp.put(NoteColumns.HAS_ATTACHMENT, cursor.getInt(5));temp.put(NoteColumns.MODIFIED_DATE, cursor.getLong(6));temp.put(NoteColumns.NOTES_COUNT, cursor.getInt(7));temp.put(NoteColumns.SNIPPET, cursor.getString(8));temp.put(NoteColumns.CONTENT, cursor.getString(9));temp.put(NoteColumns.IMAGE, cursor.getString(10));temp.put(NoteColumns.RECORD, cursor.getString(11));temp.put(NoteColumns.TYPE, cursor.getInt(12));temp.put(NoteColumns.WIDGET_ID, cursor.getInt(13));temp.put(NoteColumns.WIDGET_TYPE, cursor.getInt(14));temp.put(NoteColumns.SYNC_ID, cursor.getInt(15));temp.put(NoteColumns.LOCAL_MODIFIED, cursor.getInt(16));temp.put(NoteColumns.ORIGIN_PARENT_ID, cursor.getInt(17));temp.put(NoteColumns.GTASK_ID, cursor.getString(18));temp.put(NoteColumns.VERSION, cursor.getInt(19));} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}array.put(temp);}}try {allData.put("persondata", array);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 判断是否存在SD卡return;}File file = new File(Path() + "/" + "note.txt");PrintStream out = null; // 打印流try {out = new PrintStream(new FileOutputStream(file)); // 实例化打印流对象out.print(allData.toString()); // 输出数据} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if (out != null) { // 如果打印流不为空,则关闭打印流out.close();}}}//把sd卡中的json文件解析存储到数据库中public static void NoteJson(String str, SQLiteDatabase db) throws Exception {ContentValues mNoteValues = new ContentValues();JSONObject note = new JSONObject(str);JSONArray persondata = note.getJSONArray("persondata");for (int i = 0; i < persondata.length(); i++) {JSONObject object = persondata.getJSONObject(i);if (Integer.parseInt(object.getString("type")) == 0) {mNoteValues.put(NoteColumns.ALERTED_DATE, 0);mNoteValues.put(NoteColumns.BG_COLOR_ID,Integer.parseInt(object.getString("bg_color_id")));mNoteValues.put(NoteColumns.CREATED_DATE,Long.parseLong(object.getString("created_date")));mNoteValues.put(NoteColumns.HAS_ATTACHMENT,Integer.parseInt(object.getString("has_attachment")));mNoteValues.put(NoteColumns.MODIFIED_DATE,Long.parseLong(object.getString("modified_date")));mNoteValues.put(NoteColumns.NOTES_COUNT, 0);mNoteValues.put(NoteColumns.SNIPPET,object.getString("snippet"));mNoteValues.put(NoteColumns.CONTENT,object.getString("content"));mNoteValues.put(NoteColumns.IMAGE, object.getString("image"));mNoteValues.put(NoteColumns.RECORD, object.getString("record"));mNoteValues.put(NoteColumns.TYPE,Integer.parseInt(object.getString("type")));mNoteValues.put(NoteColumns.WIDGET_ID,Integer.parseInt(object.getString("widget_id")));mNoteValues.put(NoteColumns.WIDGET_TYPE,Integer.parseInt(object.getString("widget_type")));mNoteValues.put(NoteColumns.SYNC_ID,Integer.parseInt(object.getString("sync_id")));mNoteValues.put(NoteColumns.LOCAL_MODIFIED,Integer.parseInt(object.getString("local_modified")));mNoteValues.put(NoteColumns.ORIGIN_PARENT_ID,Integer.parseInt(object.getString("origin_parent_id")));mNoteValues.put(NoteColumns.GTASK_ID,object.getString("gtask_id"));mNoteValues.put(NoteColumns.VERSION,Integer.parseInt(object.getString("version")));mNoteValues.put(NoteColumns.PARENT_ID,Integer.parseInt(object.getString("parent_id")));db.insert(TABLE.NOTE, null, mNoteValues);}}}}

0 0
原创粉丝点击