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

mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); mBtAdapter = mBluetoothManager.getAdapter();
Java programming equivalent of environment value configured in maven run
How to select theme based template in in play 2.5?
Trim() vs IndexOf()
Should I create a Gradle project and add Drools to it or vice versa?
Draw on one side of a JPanel
use jersey2.3 tomcat8 , the page get 404
Which value is taken when bidirectionalbinding two properties with different values? [duplicate]
NullPointerException while modyfing elements on second Pane JavaFX
gradlew command not found?
Minecraft Bukkit 1.8 saving list into config.yml
Is it possible to open a file having any extension in Java? [duplicate]
Configuring FIPS mode for JRE
Exported Javafx application does not run as Jar file (Eclipse)
How to restart a Java Thread in an Android game? [duplicate]
Starting a simple JUnit / Maven project in IntelliJ Idea

Categories

HOME
winforms
testng
magnific-popup
tinymce
electron
sd-card
umd
opengl-es-2.0
datastax-java-driver
applepay
communication
adobe-analytics
quicklook
windows-azure-storage
percona
progressive-web-apps
fatal-error
samoa
autoconf
accessor
format-specifiers
hammerspoon
trading
shopware
chromebook
brunch
publish
assistant
opentype
msys2
compatibility
facebook-apps
calibre
http-digest
sqlite2
tasker
particles.js
ncalc
powershell-dsc
nxlog
google-datalayer
nstextview
worker-thread
color-profile
root-framework
grails-tomcat-plugin
bonobo
fuzzy-search
moveit
essence
bluegiga
nessus
revolution-r
two-factor-authentication
netmq
srand
ora-00900
icu4j
operation
createprocessasuser
sortedlist
lustre
block-device
gadt
relocation
mdt
codeigniter-routing
rdl
eol
funcunit
jquery-layout
bundles
html5-notifications
pyhdf
xsockets.net
cdc
excel-2003
plasma
drools-planner
jelly
word-processor
jspinclude
getmessage
online-compilation
calling-convention
jmock
google-friend-connect
coda-slider
nsviewanimation
boost-smart-ptr
text-coloring
ajax-forms
ntvdm.exe

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