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

What is the most accurate way to convert org.joda.time.LocalDateTime to java.time.LocalDateTime in scala?
Maven executable Jar throws error on start [duplicate]
write single byte using parcelbale in java
why oracle jdbc driver truncate leading zero when convert number to string
else part is not working when using arraylist
Not printing entire text file from bufferedreader
JAVA: Having trouble using DecimalFormat? Need 30.40 not 30.4 [duplicate]
Android, Fragments: Problems with Replace and remove methods
Using JTextArea while drawing with graphics
Open DatePicker with suggested date
Proper use of aggregation (OOP) with an electronic circuit class as an example
Fastest Two Number Sign Comparison Including Zero
Shortening while loop with lots of if statements
How do I swap odd elements in array of a String? Eg: Input: I Love India Output will be: I voLe dnIia
How to override the OWSM policy of a REST Binding Reference in Soa Suite 12c?
Web service with bigger strings as value in tags

Categories

HOME
urbancode
smarty
raspberry-pi
routes
amortized-analysis
dxl
win32gui
fsm
retrofit
pheatmap
facebook-php-sdk
vifm
modx-revolution
moonmail
remote-access
visual-studio-2005
visual-studio-cordova
designer
uitypeeditor
progressive-web-apps
transformation
karma-jasmine
autoconf
java-3d
accessor
dcevm
extjs5
autosys
sparse-matrix
fifo
neo4j-spatial
nouislider
.net-4.0
create-table
scorm2004
appfabric
abstract-class
streamreader
nxlog
total-commander
modelmapper
logfiles
avconv
rainbowtable
revapi
tropo
jxcore
r-forge
flutterwave
jspdf-autotable
objective-c-swift-bridge
time-and-attendance
infix-notation
log4c
holder.js
blackberry-10
phpcas
browser-link
cannon.js
simple-framework
drawbitmap
arcanist
sortedlist
web-controls
c++03
umbraco6
bigbluebutton
android-imagebutton
file-copying
applescript-objc
id3v2
jquery-layout
picturefill
android-2.2-froyo
stxxl
intentservice
c18
tws
shellexecute
html-editor
jelly
dmoz
chuck
libstdc++
manchester-syntax
spring-portlet-mvc
fireworks
gnu-prolog
mod-auth
subgurim-maps
infobox
servicehost
android-sdk-2.1
floating
gin
routedevent
preference
nintendo-ds
xetex
sustainable-pace
mdac
rtml

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