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

How to convert Scala Directory (File, Path) to Java java.io.File
JPA native query returning byte array instead of string when using concat()
Error with XML file - Android(Error parsing XML: not well-formed (invalid token))
Using Java to establish a secure connection to MySQL Amazon RDS (SSL/TLS)
Java regular expressions boundary matchers
Glassfish JDBC Connection Pooling Issue on Ubuntu
Use Math.random() to display percentage number in Java
Downloading a file from sharepoint using java program
Changing Doubles To Ints
TextureAtlas has worse filtering than Texture?
Open Text Messaging App via button click
How to verify password with Spring
Struts 1.3.10 and Commons Validator 1.4
Test Killer 310-065 for SCJP (Java)
Understanding Instructions
Clients-Server chat with AES encryption in java

Categories

HOME
amazon-ec2
appx
hashmap
octobercms
sqlite-net-extensions
elm
mouse
constraint-programming
paradox
dax
php-7.1
dosgi
fatal-error
google-static-maps
invantive-sql
beyondcompare
lldb
object-detection
dcevm
vlsi
dynamic-featured-image
intel-pin
traffic
phpfox
publish
neo4j-spatial
compatibility
gsmcomm
unboundid
react-chartjs
directx-10
galsim
preg-match-all
functor
gtrendsr
android-nestedscrollview
language-concepts
fusionpbx
io-redirection
main
hot-module-replacement
multilingual
espeak
vapor
webdriver-manager
trim
avro4s
gzipstream
grid.mvc
nand2tetris
btrace
angular-resource
rainbowtable
revapi
git-diff
android-cursor
sqldf
bind9
lync-client-sdk
root-framework
radtreelist
database-optimization
qpid
markojs
react-native-listview
tform
associative-array
rtbkit
sdhc
comobject
django-unittest
citrus-pay
libressl
computer-algebra-systems
coveralls
emailrelay
cakephp-3.1
tableau-online
responsive-images
iiviewdeckcontroller
website-monitoring
tween
jsonpickle
nstableviewcell
cctv
socketexception
document-classification
onactivityresult
xojo
reactfx
poller
kgdb
access-rights
dotnetnuke-5
excel-2003
gridcontrol
eventual-consistency
seed
sharp-repository
sabredav
browser-detection
libstdc++
ticoredatasync
ohm
xdomainrequest
servicehost
yslow
fluent-interface
paster
vc90
database-management
sef
iweb
kdbg
paul-graham

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App