java


Create JSON in 2D array to store key value pair


I have a question that would like to seek your expertise on.
I have Class call Technology and i retrieve data from the DB as object list of Technology classtechnologyList= getProjectBD().getAllTechnology();
my question is how to store data as key value pair in Json array.
This is my code
JSONArray technologyArray=new JSONArray();
for (Technology technology : technologyList) {
JSONArray gridRow=new JSONArray();
gridRow.put(technology.getTechnologyId());
gridRow.put(technology.getTechnologyName());
technologyArray.put(gridRow);
}
I need to pass this data to select option in my jsp as id and name.
ex:-[1:JAVA,2:C#...]
Try to use com.google.gson.* elements like that :
private JsonArray serializetechnologies(List<Technology> technologyList) {
JsonArray jsonArray = new JsonArray();
for (Technology technology : technologyList) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(technology.getTechnologyId()+"", technology.getTechnologyName());
jsonArray.add(jsonObject);
}
return jsonArray;
}
And if you want to get value :
for (JsonElement jsonElement : jsonArray) {
JsonObject jsonObject = (JsonObject) jsonElement;
String name = jsonObject.get(technologyX.getTechnologyId() + "").getAsString();
System.out.println("The name of technology witch Id = " + technologyX.getTechnologyId() + " is : "
+ name);
}
I hope that will help :)

Related Links

Am I Instantiating an Object from File, when I want to create an ArrayList from File? Java
Wrong file path used while running web application
Binary writing from Android-Java reading wrong from UE4-C++
Multiple file input, with stdin // Argparse4j, Java
How do I write data values from file to new output file?
Separating out REST API and implementation for Spring web services
Java Runtime Environment Or Other Issue?
Produce a Stream from a Stream and an element, Java 8
Is it a good practice to define Date fields of Persistent entities as Long type, so that we store millis in that field?
How would a dev make a JTable LOOK editable
Java 8 automatically using multicore?
Concurrency guarantees of Weak/Soft/Phantom Reference clearing
Hadoop Pipes Wordcount example: NullPointerException in LocalJobRunner
Android.support.v4 import not working
Compare 2 JSON Structures - Java
Sending an email message with highpriority using Exchange API for java

Categories

HOME
variables
deployment
magnific-popup
urbancode
raspberry-pi
rsyslog
google-oauth
elm
maven-3
microservices
ip
angular-ui-bootstrap
ebean
fancybox
multiple-records
cross-validation
vault
imacros
quicklook
jplayer
spring-tool-suite
moonmail
windows-phone-7
visual-studio-2005
designer
django-simple-history
uitypeeditor
graphlab
aurigma
excel-vba-mac
crystal-reports-2008
custom-wordpress-pages
nas
opencover
jquery-ajaxq
functional-testing
mps
google-search-api
chromium-embedded
restful-authentication
xpages-ssjs
semantic-analysis
virtualdub
uninstall
rst2pdf
subdomains
objectanimator
google-rich-snippets
galsim
textmate
accelerate-framework
plsql-psp
libvpx
streamreader
angularjs-factory
ruby-on-rails-3.1
long-polling
smartcontracts
komodoedit
clean-architecture
integrity
worker-thread
mcafee
objective-c-swift-bridge
setuptools
home-directory
phishing
py2app
mongocsharpdriver
plottable.js
php-parse-error
wapiti
cloudbees
uid
kendonumerictextbox
citrus-pay
riak-cs
inmobi
javax.mail
camanjs
sgen
jquery-layout
wp-query
valuechangelistener
undefined-reference
datagridviewcolumn
imdbpy
back-stack
random-seed
spring-validator
mhtml
multipage
hosts-file
osql
ohm
custom-backend
window-management
doh
xmlspy
cinema-4d
visitor-statistic
google-friend-connect
web-application-design
sproutcore-2
suppress
ncqrs
javap
privilege

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