在类型为org.json.JSONArray的0的值[]无法在android中转换为JSONObject?

时间:2015-10-08 20:45:40

标签: java php android json

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

10%9

php文件

    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();
    }

和json数组

<?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);
?>

它告诉我的错误是这个     值[[],{“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

1 个答案:

答案 0 :(得分:1)

它给出了这个错误,因为数组中的第一个对象不是JSONObject,而是跟随4个JSONObjects的JSONArray。

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

如您所见,您希望它始终是JSONObject。

JSONObject c = peoples.getJSONObject(i);

预测列表中的第一个JSONArray。