android


Android: Database to JSON [closed]


How to generate a JSON file using a populated database. As of now, I can only retrieve them 1 at a time.
For example, I have a table of people that contains an id, first name, and last name. How can I loop through all of them and end up with a JSON file that contains all of the people?
[
{"id":1,"firstName":"Johnny","lastName":"Storm"},
{"id":2,"firstName":"Sue","lastName":"Storm"},
{"id":3,"firstName":"Reed","lastName":"Richards"},
{"id":4,"firstName":"Ben","lastName":"Grimm"}
]
Use cursor to get all the data in database then use
JsonArray jsonArray = new JsonArray();
cursor.moveToNext();
while (cursor.moveToNext()) {
JsonObject jsonObject=new JsonObject();
jsonObject.put("id",cursor.getInt(cursor.getIndex(<your id column name>)))
jsonObject.put("firstName",cursor.getString(cursor.getIndex(<your firstName column name>)))
//Do same for all the other columns
jsonArray.add(jsonObject);
}
Now your JSONArray will be [{"id":1,"firstName":"Johnny","lastName":"Storm"},{"id":2,"firstName":"Sue","lastName":"Storm"},{"id":3,"firstName":"Reed","lastName":"Richards"},{"id":4,"firstName":"Ben","lastName":"Grimm"}]

Related Links

Clossing expanded child views in ExpandableListActivity
Is this code Correct ? Send Email Via Android
java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication
Can I catch Exception in C in android NDK?
how to use Service
Android VideoView repetition
How to read more than one column in database by cursor and fill into a list?
Is there a way to set up an initial orientation for the Android device
How to draw touch-enabled rect in Android
changing font style dynamically
How to create Vertical Carousel in android?
How to disable/hide three-dot indicator(Option menu indicator) on ICS handsets
progressbar in openGL android
How to handle the error: NativeStart.main(String[]) line: not available [native method] in Android
Display preview of camera using external camera
paste image on another image in android

Categories

HOME
windows
c++11
codenvy
css3
gpu
gradient
cq5
mips
webix
opengl-3
flume
modal-dialog
joomla3.0
spin
automatic-ref-counting
esoteric-languages
null-pointer
firefox-addon
video-editing
html5-appcache
digital-signature
identity-management
bourbon
replication
spring-async
named-entity-recognition
inno-download-plugin
resourcebundle
ckfinder
mat
sammy.js
keras-layer
browser-extension
roblox
raft
health-monitoring
avcapturedevice
angular-http
vaadin-elements
magento-2.0.7
robomongo
arules
quicktime
cd-burning
mathcad
ziparchive
webjars
installshield-2010
qiime
omniauth-facebook
dock
alloy-ui
telepat
fail2ban
kubuntu
android-broadcast
shopizer
rapidsvn
formview
paw
lambda-architecture
grails-domain-class
fpdi
menubar
drupal-nodes
rfc5322
code-duplication
cocoalibspotify-2.0
web-notifications
mapping-by-code
database-create
oracle-enterprise-linux
user-tracking
winmain
rmdir
clrstoredprocedure
http-daemon
maven-1
easyb
echo3
purepdf
interop-domino
dojo-dnd
flvplayback
soappy
ifilter
wspbuilder

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App