在android系统中,org.json.JSONArray类型的值[]at 0不能转换为JSONObject


Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject in android?

我试图从android访问json数组。但它显示了一个例外。这是我的android代码用于检索json数组:

    protected void showList(){
      final String TAG_RESULTS="result";
       final String TAG_USERNAME="username";
     final String TAG_NAME = "message_recd";
      final String TAG_ADD ="message_sent";
       ArrayList<HashMap<String, String>> personList;
       personList = new ArrayList<HashMap<String,String>>();
       //for tesitng
       JSONObject jObject=null;
       //
    try {
        //for testing
        //
       JSONObject json = new JSONObject(myJSON);
        JSONArray peoples =json.getJSONArray("emparray");
        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String name=null, address=null;
            name = c.getString("user_id");
            address = c.getString("crtloc_lat");
            HashMap<String,String> persons = new HashMap<String,String>();
            persons.put("user_id",name);
            persons.put("crtloc_lat",address);
            personList.add(persons);
            Toast.makeText(MapsActivity.this, "woow id"+name, Toast.LENGTH_SHORT).show();
        }
    } catch (JSONException e) {
        Log.e("errore",e.toString());
        e.printStackTrace();
    }
}
    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{
            @Override
            protected String doInBackground(String... params) {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
              //  nameValuePairs.add(new BasicNameValuePair("username", fName));
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://abh.netai.net/abhfiles/searchProfession.php");
                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");
                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "'n");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }
            @Override
            protected void onPostExecute(String result){
                myJSON=result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
php文件

<?php
require "config.php";
$con = mysqli_connect(HOST,USER,PASS,DB);
$pro_id=0;
$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);
?>

和json数组

[[],{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769566","crtloc_lng":"72.361031"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"},{"user_id":"86","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]

显示的错误是这样的价值[[],{"user_id":"77","crtloc_lat":"34.769638","crtloc_lng":"72.361145"},{"user_id":"76","crtloc_lat":"34.769749","crtloc_lng":"72.361168"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}]org.json.JSONArray不能转换为类型的JSONObject

它会给你这个错误,因为数组中的第一个对象不是JSONObject,而是继4个JSONObject之后的JSONArray。

[
[],
{
    "user_id": "77",
    "crtloc_lat": "34.769638",
    "crtloc_lng": "72.361145"
},]

正如你所看到的,你希望它总是一个JSONObject。

JSONObject c = peoples.getJSONObject(i);