How to extract values from JSONObject in android
Hope this may help you
JSONObject jsonObject=new JSONObject(result); jsonObject=jsonObject.getJSONObject("1"); Double lat=jsonObject.getDouble("latitude"); Toast.makeText(getBaseContext(),""+ lat, Toast.LENGTH_LONG).show();
Better create a valid JSONArray
to make things easier. When you create the JSON
string , don't append the index. I believe your are reading the data from sqlite and generating the JSON
string.
Also change the outer {
(curly brace) to [ (square bracket) to represent as Array
element. So your json string will be like this
[{ "sno": "10", "latitude": "31.249441437085455", "longitude": "75.70003598928452"},{ "sno": "11", "latitude": "31.249398442090207", "longitude": "75.70003397762775"}]
Then , you can simply get its as JSONArray
JSONArray jsonArray = new JSONArray(jsonString);
And then read each element in array item
for(int index = 0;index < jsonArray.length(); index++) { JSONObject jsonObject = jsonArray.getJSONObject(index); }
To get one of your inner JSONObject elements, you'll need to get it from the outer one using getJSONObject(String key).
Sample code:
JSONObject item1 = myJson.getJSONObject("1");//item1 now contains {sno:10,latitude:31.2...etc}//now you can get the individual values out of the resulting JSON Object//for example, getting the latitudedouble lat = item1.getDouble("latitude");