मेरे मामले में यह था POJO वर्गों के ArrayList नोट
private String mNoteTitle;
private int mFingerIndex;
private Point mNoteCoordinates;
public Note(String noteTitle, int fingerIndex, Point noteCoordinates) {
this.mNoteTitle = noteTitle;
this.mFingerIndex = fingerIndex;
this.mNoteCoordinates = noteCoordinates;
}
मैनुअल कहते हैं JSONObject केवल निम्न प्रकारों का समर्थन करता है: ऑब्जेक्ट: एक JSONObject, JSONArray, स्ट्रिंग, बूलियन, पूर्णांक, लांग, डबल, नल, या शून्य। NaNs या infinities नहीं हो सकता है। इसलिए, मुझे अपनी नोट क्लास को समर्थित ऑब्जेक्ट्स में तोड़ना चाहिए।
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray();
for(Note note: chordShape.getNotes()){
JSONObject singleNoteJsonObject = new JSONObject();
try {
singleNoteJsonObject.put(SHAPE_NOTE_TITLE, note.getNoteTitle());
singleNoteJsonObject.put(SHAPE_NOTE_FINGER_INDEX, note.getFingerIndex());
singleNoteJsonObject.put(SHAPE_NOTE_X, note.getNoteCoordinates().x);
singleNoteJsonObject.put(SHAPE_NOTE_Y, note.getNoteCoordinates().y);
} catch (JSONException e){
e.printStackTrace();
}
jsonArray.put(singleNoteJsonObject);
}
पैक JSONObject में सरणी बनाई गई।
try {
json.put(SHAPE_NOTES, jsonArray);
Log.i(TAG, json.toString());
} catch (JSONException e){
e.printStackTrace();
}
स्ट्रिंग बनाएं। ContentValues में
String notesList = json.toString();
रखें बनाई स्ट्रिंग, मेरे मामले में कारण यह Android एप्लिकेशन है
if(notesList.length() > 0){
contentValues.put(DatabaseHelper.SHAPE_NOTES_LIST, notesList);
}
और जब मैं SQLite डेटाबेस से मानों पढ़ना चाहिए।
ArrayList<Note> notes = new ArrayList<>();
while(cursor.moveToNext()){
JSONObject jsonNotes = null;
try {
jsonNotes = new JSONObject(cursor.getString(cursor.getColumnIndex(DatabaseHelper.SHAPE_NOTES_LIST)));
} catch (JSONException e){
e.printStackTrace();
}
if(jsonNotes != null){
Log.i(TAG, jsonNotes.toString());
JSONArray jsonArray = jsonNotes.optJSONArray(SHAPE_NOTES);
for(int i = 0; i < jsonArray.length(); i++){
Note note = null;
JSONObject arrayObject = null;
try {
arrayObject = jsonArray.getJSONObject(i);
} catch (JSONException e){
e.printStackTrace();
}
if(arrayObject != null){
try {
note = new Note(
arrayObject.getString(SHAPE_NOTE_TITLE),
arrayObject.getInt(SHAPE_NOTE_FINGER_INDEX),
new Point(
arrayObject.getInt(SHAPE_NOTE_X),
arrayObject.getInt(SHAPE_NOTE_Y)
)
);
} catch (JSONException e){
e.printStackTrace();
}
}
if(note != null){
notes.add(note);
}
}
}
}
cursor.close();
यह वास्तव में दिलचस्प लगता है! मैं इसे देख लूंगा और देख सकता हूं कि यह कैसे काम करता है। – Achint
यह कमाल है। – leparlon
हाय क्या आप stringreadfromsqlite के बारे में अधिक समझा सकते हैं। – max