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

标签 java php android json

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

    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 org.json.JSONArray 类型的 ":"72.361168"},{"user_id":"87","crtloc_lat":"33.697117","crtloc_lng":"72.976631"}] 无法转换为 JSONObject

最佳答案

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

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

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

JSONObject c = peoples.getJSONObject(i);

预期列表中的第一个 JSONArray。

关于java - org.json.JSONArray 类型为 0 处的值 [] 无法在 android 中转换为 JSONObject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025647/

相关文章:

php - 使用 $this 回调函数

android - 预期的 URL 方案 'http' 或 'https' 但未找到冒号

android - 在另一个 Acitivity 获得焦点后返回到 SurfaceView

android - 在Android应用上搜索和播放YouTube视频

java - eclipse 氧气/氖气 : Unable to build ANT project

java - 如何在 Netbeans 7 中附加缺少的 Java JDK 源

java - 为什么我不能在 Struts2 中将一个 Action 重定向到另一个 Action ?

php - 使用 Gmail 的 SMTP 服务器每天发送 1000 多封电子邮件

php - 协会的 Doctrine postLoad 事件

java - 官方网站下载ldapjdk.jar和netscape.jar