jsonファイルの生成とjsonファイルの解析


開発ではjsonファイルの解析やデータベース内のデータをjsonファイルに書く形で格納することが多い.
コードは次のとおりです.
//          json        SD  
public static void backupsNote(SQLiteDatabase db) {
		JSONObject allData = new JSONObject(); //       JSONObject
		JSONArray 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 block
					e.printStackTrace();
				}
				array.put(temp);
			}
		}
		try {
			allData.put("persondata", array);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.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 block
			e.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);
				}
			}
		}
	}