【问题标题】:How To Get JsonArrayRequest JSONArray Response To JSONObject如何获取对 JSONObject 的 JsonArrayRequest JSONArray 响应
【发布时间】:2016-06-02 13:10:34
【问题描述】:

从昨天开始我就一直遇到这个问题,从我的服务器获取我的数据。我在 POSTMAN 中测试我的 php 代码并将格式更改为 JSON,我什至在 JSONLint 上测试它并返回有效的 json。我注意到它返回 JSONObject 而不是 JSONArray (据我了解 JSONArray 以 [ 开头)。

这是我的 java 代码:orgFragment.java

public void parseJSONData(){
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    JsonArrayRequest jsonArray = new JsonArrayRequest(ServerScripts.PHP_SCRIPT_PATH + ServerScripts.PHP_GET_FEEDS, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            if (response.length() > 0){
                try {
                    organizationDataList.clear();
                    for (int i = 0; i < response.length(); i++ ){
                        JSONObject jsonObject = response.getJSONObject(i);
                        OrganizationData organizationData = new OrganizationData();

                        //SQL TABLE NAME
                        if (!jsonObject.isNull("organizationName")){
                            organizationData.orgName = jsonObject.getString("organizationName");
                        }

                        if (!jsonObject.isNull("organizationType")){
                            organizationData.orgType = jsonObject.getString("organizationType");
                        }

                        if (!jsonObject.isNull("organizationDescription")){
                            organizationData.orgDesc = jsonObject.getString("organizationDescription");
                        }

                        if (!jsonObject.isNull("organizationCategory")){
                            organizationData.orgCategory = jsonObject.getString("organizationCategory");
                        }

                        if (!jsonObject.isNull("organizationCurrentMembers")){
                            organizationData.orgCurrentMembers = jsonObject.getInt("organizationCurrentMembers");
                        }

                        if (!jsonObject.isNull("organizationMaxMembersNo")){
                            organizationData.orgMaxMembers = jsonObject.getInt("organizationMaxMembersNo");
                        }
                        //ADD
                        organizationDataList.add(organizationData);
                    }
                        //NOTIFY
                        orgListAdapter.notifyDataSetChanged();

                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
                    Log.e("JSON Parser", "Error parsing data " + e.toString());
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String getErrorMsg = error.toString();
            Toast.makeText(getContext(),"Failed to Fetch Data" + getErrorMsg,Toast.LENGTH_LONG).show();
        }

    });
    requestQueue.add(jsonArray);

}

错误:我刚刚从 Logcat 截取了 json 错误响应 org.json.JSONObject 类型的“成功”} 无法转换为 JSONArray

【问题讨论】:

    标签: java php json jsonobject jsonresult


    【解决方案1】:

    我只是修改我的 php 代码,返回一个 JSONArray。

    //if($_SERVER['REQUEST_METHOD'] == ''){
    
    
         require('dbConnect.php');
    
         $SQLi_ORG_FEEDS = "SELECT organizationName,organizationDescription,organizationCategory,
                            organizationCurrentMembers,organizationMaxMembersNo,organizationType FROM org_information";
    
         $query = mysqli_query($dbConnect,$SQLi_ORG_FEEDS) or die("Error".mysqli_error($dbConnect));
    
         $checkRow = mysqli_num_rows($query);
    
         $response = array();
    
         if($checkRow > 0){
             while ($getRecord = mysqli_fetch_array($query)) {
                $response[] = $getRecord;
    
            /*array_push($response,array(
                "organizationName"=>$getRecord['organizationName'],
                "organizationDescription"=>$getRecord['organizationDescription'],
                "organizationCategory"=>$getRecord['organizationCategory'],
                "organizationCurrentMembers"=>$getRecord['organizationCurrentMembers'],
                "organizationMaxMembersNo"=>$getRecord['organizationMaxMembersNo'],
                "organizationType"=>$getRecord['organizationType']));*/
             }
    
                //$response['success'] = 'success';
                echo json_encode($response);
                //echo json_encode(array("response"=>$response));
         }
         else {
                $response['failed'] = 'failed';
                echo json_encode($response);
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      相关资源
      最近更新 更多