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

How to filter and execute specific Gradle tasks?
Android : Stop Face Tracking from non-activity(Tracker) class
Picasso not loading some images to RecyclerView
Quickblox issue with sign up and login
How to Create a video in Android?
Drools Android-integrated project error
Android : How to reduce the size of the selected file
Explaination behind weird behavior with AudioTrack for Android
Buttons move with gmaps api move
OkHttp authenticator
I get a JSON exception
Using PendingIntent() to request location updates when running on an O device
Change Spinner adapter OnItemSelectedListener arguments
API for moving speed (location) without a map
changing ImageView properties several times
Nothing appears in my Xamarin application

Categories

HOME
beautifulsoup
codenvy
toolbox
recordrtc
okhttp
google-chrome-app
why3
naivebayes
libusb
linear-algebra
scsi
pyramid
oledb
apache2.4
simpleitk
springfox
libigl
backpack-for-laravel
scriptella
data.stackexchange.com
slidetoggle
android-maps
firebase-analytics
rpgle
iis-express
calculation
mobx
android-sugarorm
pop3
timing
protein-database
sim-card
odroid
recurrent-neural-network
loading
mongoose-schema
mcustomscrollbar
azure-arm
visa
outputcache
importerror
clang++
gitlist
yii2-api
google-cse
qtranslate
wpas
compiler-design
skin
css-transforms
baidu
javafx-css
slackware
autosar
flex-monkey
model-associations
gcloud-ruby
turbo-prolog
nullable
pushbots
cycle2
mobiledoc-kit
concept-insights
crouton-os
spring-retry
xenomai
vigenere
viola-jones
wso2greg
liquid-layout
bufferedinputstream
raygun
gii
template-deduction
conditionaltagsupport
kendo-dataviz
modular
node-blade
diem
resource-management
qi4j
xcode3.2
agility.js
pinch
google-ajax-api
http-daemon
commerceserver2007
filesystemobject
glui
ifilter

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