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

neo4j null pointer exception while saving via #Repository
how to take last element from Map<String,Integer>
How to avoid the trimming of these trailing whitespaces?
Gradle Module Build Ordering
Dynamically register listeners in swing
Servlet is not working in Server AWS
Launching a web-link from onClickListener
Context initialization failed (Cannot find the declaration of element 'beans') - Spring MVC
Implementing state pattern with Graph data structure
splunk sdk java post process search
Jax RS Authorization
Input from a JTextField and compare it to the string inputted and then move onto the next JFrame
How to get sharepoint web service?
Deleting data results related to a query using Java API Elasticsearch
Property values to select tag in struts 1.x
Why my APP load a view before main view?

Categories

HOME
vim
image
variables
ngrx
path-finding
relative-path
read-eval-print-loop
rsyslog
gis
fingerprint
ezpublish
django-imagekit
directx
dacpac
floating-action-button
n-gram
phaser
foselasticabundle
google-static-maps
flux
amazonsellercentral
java-3d
abi
ehcache
accessor
extjs5
language-agnostic
cultureinfo
blazemeter
opennlp
django-storage
facebook-access-token
brunch
windows-error-reporting
buck
msys2
preg-match
galsim
calibre
android-kernel
mozilla
http-referer
environment-modules
mapdb
ios5
retina-display
swift3.0.2
mixture-model
hilbert-curve
user-controls
awt
jvm-languages
no-www
reactive-cocoa-5
wptoolkit
flickr-api
powercli
boost-preprocessor
quartz-composer
setuptools
wdf
log4c
ado.net-entity-data-model
clang-static-analyzer
connect-by
lua-5.1
dlna
itextpdf
system.management
uid
freedesktop.org
maven-tomcat-plugin
r-tree
prerequisites
google-places
dukescript
myo
apache-commons-fileupload
clipperlib
proj4js
c++03
dd
winddk
uitouch
fluentautomation
undefined-reference
dealloc
c18
mylyn
plasma
pygit2
limejs
typoscript2
cufon
pydot
tomcat-valve
mysql-error-1005
fluent-interface
dentrix
sustainable-pace
mdac

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