java


How can I retrieve a String array from JSONObject? [duplicate]


This question already has an answer here:
How to parse JSON in Java
26 answers
My JSONArray contains various JSONObjects which look like this:
{"nearby":0,"x":0,"name":["OPS","BHU"],"y":0}
{"nearby":0,"x":0,"name":["None"],"y":1}
{"nearby":0,"x":0,"name":["None"],"y":2}
{"nearby":0,"x":0,"name":["None"],"y":3}
{"nearby":0,"x":0,"name":["None"],"y":4}
{"nearby":0,"x":0,"name":["None"],"y":5}
{"nearby":0,"x":0,"name":["None"],"y":6}
{"nearby":0,"x":1,"name":["None"],"y":0}
{"nearby":0,"x":1,"name":["None"],"y":1}
{"nearby":0,"x":1,"name":["None"],"y":2}
{"nearby":0,"x":1,"name":["DDF","THG"],"y":3}
{"nearby":0,"x":1,"name":["None"],"y":4}
{"nearby":0,"x":1,"name":["None"],"y":5}
{"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6}
I want to retrieve values corresponding to name key in JSONObject and from an array like this:
JSONObject jo= jsonArray.getJSONObject(i);
String nearby= jo.getString("nearby");
String xString= jo.getString("x");
int x=Integer.parseInt(xString);
String yString= jo.getString("y");
int y=Integer.parseInt(yString);
String[][] name=new String[7][7];
name[x][y]= ????????
Such that the output be like:
name[0][0]=["OPS","BHU"]
name[0][1]=["None"]
name[0][2]=["None"]
name[0][3]=["None"]
name[0][4]=["None"]
name[0][5]=["None"]
name[0][6]=["None"]
name[1][0]=["None"]
name[1][1]=["None"]
name[1][2]=["None"]
name[1][3]=["DDF","THG"]
name[1][4]=["None"]
name[1][5]=["None"]
name[1][6]=["ABC","DEF","XYZ"]
I hope this is what you are looking for,
var arr = [
{"nearby":0,"x":0,"name":["OPS","BHU"],"y":0},
{"nearby":0,"x":0,"name":["None"],"y":1},
{"nearby":0,"x":0,"name":["None"],"y":2},
{"nearby":0,"x":0,"name":["None"],"y":3},
{"nearby":0,"x":0,"name":["None"],"y":4},
{"nearby":0,"x":0,"name":["None"],"y":5},
{"nearby":0,"x":0,"name":["None"],"y":6},
{"nearby":0,"x":1,"name":["None"],"y":0},
{"nearby":0,"x":1,"name":["None"],"y":1},
{"nearby":0,"x":1,"name":["None"],"y":2},
{"nearby":0,"x":1,"name":["DDF","THG"],"y":3},
{"nearby":0,"x":1,"name":["None"],"y":4},
{"nearby":0,"x":1,"name":["None"],"y":5},
{"nearby":1,"x":1,"name":["ABC","DEF","XYZ"],"y":6},
]
var resultArr = [[],[]];
$.each(arr, function (key, item) {
resultArr[item.x][item.y]=item.name;
});
console.log(resultArr[0][0])
console.log(resultArr[0][1])
console.log(resultArr[0][5])
console.log(resultArr[1][4])
console.log(resultArr[1][6])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Related Links

Get GMT Time in Java
Mapping primitive classes (String, Boolean, etc) to each other with Dozer
Adding links to REST Responses
counting the number of documents containing a specific term
How to call servlet or web service from JSF backing bean?
java support online certificate status protocol
Java Serialization Exception Message
Manipulate a URL with a spinner
JavaBean - Bound properties
Repainting in Java Swing with containers
mysterious Null Pointer Exception with Simple XML serializer.write
Version property of Parent isn't incrementing if I update child
Creating a socket server which allows multiple connections via threads and Java
Jasper Report Generates a PDF and then Glassfish crashes/shutsdown
Is this error about not finding a variable a scope problem?
Printing with excel and java or others

Categories

HOME
listview
gerrit
activiti
isabelle
relative-path
relayjs
q
framework7
bookshelf.js
webpack-2
ios-charts
append
cross-validation
esper
facebook-php-sdk
communication
google-cloud-ml
php-7.1
visual-studio-cordova
django-simple-history
systemc
after-effects
jsdoc
connection-string
jqwidget
textfield
crosstab
p-value
publish
ejabberd-module
libuv
google-cloud-endpoints-v2
xquery-3.0
catch-all
perlin-noise
usbserial
host
dartium
serve
neuroscience
xenforo
sas-visual-analytics
unoconv
grid.mvc
http-live-streaming
eclipse-gef
jquery-validate
celery-task
togetherjs
rotativa
lua-5.1
sigabrt
actionbardrawertoggle
qgraphicsview
oauth2client
tcpserver
jms2
mutation-observers
simple-framework
apache-commons-fileupload
unity3d-gui
remobjects
oberon
skos
e10s
nsmutabledictionary
angularjs-ng-click
fpml
mesa
tld
poller
braille
balanced-payments
geos
ruby-datamapper
back-stack
spring-validator
e4x
armcc
uiviewanimation-curve
work-stealing
subgurim-maps
jmock
cxxtest
.nettiers
modelstate
ext3
ncqrs
brewmp
memory-size
multiple-languages
signal-handling

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