0

Below is my Android code which is supposed to retrieve the json array. But it shows an exception that "json array cannot be converted to json object". What is wrong with this code?

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

// testing works ...

// ... until here
for (int i = 0; i < jArray.length(); i++) {
    json = jArray.getJSONObject(i);

    String id = null, loc = null;
    id = json.getString("user_id");

    loc = json.getString("crtloc_lat");

    HashMap<String,String> persons2 = new HashMap<String,String>();
    persons2.put("uder_id",id);
    persons2.put("crtloc_lat",loc);
    personList.add(persons2);
    mylist.add(persons2);
    Toast.makeText(MapsActivity.this, "waw id is"+id, Toast.LENGTH_SHORT).show();
}

PHP file

 <?php
require "config.php";
$con = mysqli_connect(HOST,USER,PASS,DB);

$pro_id=2;
$sql="SELECT user.user_id, current_location.crtloc_lat,current_location.crtloc_lng FROM user INNER JOIN current_location 
where user.user_id=current_location.user_id AND user.pro_id='$pro_id'";



  $result = mysqli_query($con, $sql) or die("Error in Selecting " . mysqli_error($con));

    //create an array
    $emparray[] = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    //close the db connection
    mysqli_close($con);
?>

and the following is my json array

[[],{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769547","crtloc_lng":"72.361504"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"},{"user_id":"86","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]
3
  • Can you show the expected JSON object you are trying to parse? Commented Oct 8, 2015 at 10:33
  • Looks like result is an json array so you have to change to JSONArray json = new JSONArray(result); Commented Oct 8, 2015 at 10:40
  • @CarefreeCrayon please have a look to my edited question.thnx Commented Oct 8, 2015 at 16:02

1 Answer 1

2

You should check your JSON as string to understand where is the problem, in any case if result is a JSON Array (like [ .... ] ) your first line is wrong

JSONObject json = new JSONObject(result);
JSONArray jArray = json.getJSONArray("emparray");

It should be

JSONArray jArray = json.getJSONArray(result);

But to be sure you should post your JSON string...

There are also another think that makes me a little bit confused, in JAVA a JSONObject is created around an HashMap (as you can see here) and a JSONArray is very close to an Array of HashMap...

I think that what are you tring to do is a bit useless...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.