Simon A. Callaghan Jensen Simon A. Callaghan Jensen - 1 month ago 7x
Android Question

Parsing JSONArray with Double in Android Studio

I am trying to parse

from a MySQL Database in Android Studio, but i get the following exception:

org.json.JSONException: Value {"success":false,"0":{"success":false,"marker_id":null,"lat":null,"lng":null,"snippet":null},"1":{"success":false,"marker_id":null,"lat":null,"lng":null,"snippet":null},"2":{"success":false,"marker_id":null,"lat":null,"lng":null,"snippet":null},"3":{"success":false,"marker_id":null,"lat":null,"lng":null,"snippet":null}} of type org.json.JSONObject cannot be converted to JSONArray

What i am trying to do is to parse several markers containing double values for LatLng, and then placing those markers in Google Maps.

Code for placing the markers and making the request:

Response.Listener<String> responseListener = new Response.Listener<String>() {
public void onResponse(String response) {
try {

JSONArray jsonResponse = new JSONArray(response);
Log.i("RESPONSE:", "[" + jsonResponse + "]");

for (int i = 0; i < response.length(); i++) {

//boolean success = jsonResponse.getBoolean("success");
//double lat = jsonResponse.getDouble("lat");
//double lng = jsonResponse.getDouble("lng");
//int snippet = jsonResponse.getInt("snippet");

//LatLng Mplace = new LatLng(lat, lng);

//MarkerOptions marker = new MarkerOptions().position(Mplace).title("Marker").snippet("Snippet:" + "0");


} catch (JSONException e) {
Log.i("RESPONSE:", "[" + response + "]");
"Error: " + e.getMessage(),



MarkerRequest markerRequest = new MarkerRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(AllMarkersActivity.this);

For now i have commented the actual placing of the markers and that is when i get the exception shown above. When i remove the commenting i get this red-text message:

getDouble (int) cannot be applied to (java.lang.String)

Here is my Request code:

public class MarkerRequest extends StringRequest {
private static final String MARKER_REQUEST_URL = "";
private Map<String, String> params;
//private JSONObject data = new JSONObject("");

public MarkerRequest(Response.Listener<String> listener){
super(Request.Method.POST, MARKER_REQUEST_URL, listener, null);
params = new HashMap<>();
public Map<String, String> getParams() {
return params;

And here is my PHP script:

$con = mysqli_connect("myhost", "myuser", "mypass", "mydatabase");

/*$statement = mysqli_prepare($con);

$sql = "SELECT * FROM markers ORDER BY marker_id ASC ";
$strSQL = "SELECT * FROM `markers` ORDER BY marker_id ASC ";

$objQuery = mysql_query($statement, $strSQL) or die(mysql_error());
$arrRows = array();
$arryItem = array();*/

$statement = mysqli_prepare($con, "SELECT * FROM markers ORDER BY marker_id ASC ");
//mysqli_stmt_bind_result($statement, $colmarker_id, $collat, $collng, $colsnippet);

//$response = array();
$arrRows = array();
$arryItem = array();
//$response["success"] = false;
$arrRows["success"] = false;
$arryItem["success"] = false;

while(mysqli_stmt_fetch($statement)) {
$arryItem["marker_id"] = $arr["marker_id"];
$arryItem["lat"] = $arr["lat"];
$arryItem["lng"] = $arr["lng"];
$arryItem["snippet"] = $arr["snippet"];
$arrRows[] = $arryItem;

echo json_encode($arrRows);

So my question can be narrowed down to this:

How do i parse a
(within an array?), and how do i parse a double value within that

It would be nice with a code example, as i am still learning :)



As your response contain JSON Object and the outer JSON object contain inner JSON objects. So, first parse it in JSON Object then get Lat and lng. Try this code:

    JSONObject jsonObject = new JSONObject(response);
    Iterator<String> keys = jsonObject.keys();
    while( keys.hasNext() )
        String key =;
                if(!key.equals("success")){   // or use this if your keys are always in digits form.. if(TextUtils.isDigitsOnly(key))
                    Log.v("category key", key);
                    JSONObject innerJObject = jsonObject.getJSONObject(key);
                    String lat_str = innerJObject.getString("lat");
                    double lat = 0.0, lng = 0.0;

                    if(!TextUtils.isEmpty(lat_str) &&      TextUtils.isDigitsOnly(lat_str))
                        lat = Double.parseDouble(lat_str);

                    String lng_str = innerJObject.getString("lng");

                    if(!TextUtils.isEmpty(lng_str) && TextUtils.isDigitsOnly(lng_str))
                        lng = Double.parseDouble(lng_str);


Add exception handling in this block. This might help!