【问题标题】:Android: JSONException: Value null at VesselList of type org.json.JSONObject$1 cannot be converted to JSONArrayAndroid:JSONException:org.json.JSONObject$1 类型的 VesselList 的值为 null 无法转换为 JSONArray
【发布时间】:2015-09-11 04:41:25
【问题描述】:

我正在尝试解析 JSOn 对象,但有时它运行正常,有时会出现以下错误:

org.json.JSONException:类型为 VesselList 的值为 null org.json.JSONObject$1 无法转换为 JSONArray

public void getCompanyDeatails() 
    {
        String strUrl_companyDeatls = "http://103.24.4.60/CLASSNK1/MobileService.svc/Get_Company/Sync_Time/"+strSync_Time+"/Authentication_Token/"+str_Authentication_Token;
        Log.e("strUrl_companyDeatls ", " = " + strUrl_companyDeatls);
        InputStream inputStream = null;
        try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet(strUrl_companyDeatls));
            inputStream = httpResponse.getEntity().getContent();

            if (inputStream != null)
                strResult = convertInputStreamToString(inputStream);
            else
                strResult = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
        String jsonStr = strResult;
        Log.e("jsonStr ", " = " + jsonStr);

        if (jsonStr != null)
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                String jsonResult = jsonObj.toString().trim();
                Log.e("jsonResult ", " = " + jsonResult);

                JSONObject companyList = jsonObj.getJSONObject("Get_CompanyResult");
                Log.e("companyList ", " = " + companyList.toString());

                JSONArray jarr = jsonObj.getJSONArray("CompanylList");
                Log.e("jarr ", " = " + jarr.toString());

                for (int i = 0; i < jarr.length(); i++) {
                    JSONObject jobCompanyDetails = jarr.getJSONObject(i);

                    str_CompanyId = jobCompanyDetails.getString("Company_ID");
                    str_CompanyName = jobCompanyDetails.getString("Company_Name");

                    Log.e("str_CompanyId ", " = " + str_CompanyId);
                    Log.e("str_CompanyName ", " = " + str_CompanyName);

                    if (dbhelper.isTitleExist(str_CompanyId)) {
                        //Upadte
                        dbhelper.updatedetails(str_CompanyId, str_CompanyName);
                        Log.e("Data updated in ", "Company Table !!");
                    } else {

                        //insert
                        dbhelper.insertCompany(str_CompanyId, str_CompanyName);
                        Log.e("Data inserted in ", "Company Table !!");
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
    }

这是我的 JSON 字符串

{"Get_CompanyResult":{"CompanylList":[{"Company_ID":93,"Company_Name":"SeaChange"},{"Company_ID":97,"Company_Name":"VM 2"}],"Sync_Time":"2015-09-11 12:44:17.533"}}

这段代码对吗?

【问题讨论】:

  • 我假设这条线? JSONArray jarr = jsonObj.getJSONArray("CompanylList");
  • 是的,我尝试过类似 JSONArray jarr = jsonObj.getJSONArray("CompanylList");但是我收到 JSONObject$1 的错误无法转换为 JSONArray

标签: android


【解决方案1】:

这里:

JSONArray jarr = jsonObj.getJSONArray("CompanylList");

由于CompanylList JSONArray 在Get_CompanyResult JSONObject 内部而不是jsonObj 的main,因此导致问题的行。

从companyList JSONObject 获取CompanylList JSONArray:

JSONArray jarr = companyList.getJSONArray("CompanylList");

【讨论】:

  • 我也试过这样 JSONArray jarr = companyList.getJSONArray("CompanylList");为什么会出现异常 JSONObject$1 无法转换为 JSONArray
  • JSONArray jarr = companyList.getJSONArray("CompanylList");在这一行出现错误
  • @androidTag:但在发布的 json 中有问题 CompanylList 是 JSONArray。尝试使用JSONObject jObject = companyList.getJSONObject("CompanylList"); 并与我们分享jObject 字符串
【解决方案2】:

根据此处更改您的代码:

public void getCompanyDeatails() {
        String strUrl_companyDeatls = "http://103.24.4.60/CLASSNK1/MobileService.svc/Get_Company/Sync_Time/" + strSync_Time + "/Authentication_Token/" + str_Authentication_Token;
        Log.e("strUrl_companyDeatls ", " = " + strUrl_companyDeatls);
        InputStream inputStream = null;
        String strResult = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse httpResponse = httpclient.execute(new HttpGet("strUrl_companyDeatls"));
            inputStream = httpResponse.getEntity().getContent();

//            strResult = "{\\\"Get_CompanyResult\\\":{\\\"CompanylList\\\":[{\\\"Company_ID\\\":93,\\\"Company_Name\\\":\\\"SeaChange\\\"},{\\\"Company_ID\\\":97,\\\"Company_Name\\\":\\\"VM 2\\\"}],\\\"Sync_Time\\\":\\\"2015-09-11 12:44:17.533\\\"}}";
            String jsonStr=null;
            if (inputStream != null) {
                strResult = convertInputStreamToString(inputStream);
                jsonStr = strResult;
            }

            Log.e("jsonStr ", " = " + jsonStr);

            if (jsonStr != null) {
                JSONObject jsonObj = new JSONObject(jsonStr);

                String jsonResult = jsonObj.toString().trim();
                Log.e("jsonResult ", " = " + jsonResult);

                JSONObject companyList = jsonObj.getJSONObject("Get_CompanyResult");
                Log.e("companyList ", " = " + companyList.toString());

                JSONArray jarr = companyList.getJSONArray("CompanylList");
                Log.e("jarr ", " = " + jarr.toString()); // error was here. You need to use companyList json object because it contains company list.

                for (int i = 0; i < jarr.length(); i++) {
                    JSONObject jobCompanyDetails = jarr.getJSONObject(i);

                    String str_CompanyId = jobCompanyDetails.getString("Company_ID");
                    String str_CompanyName = jobCompanyDetails.getString("Company_Name");

                    Log.e("str_CompanyId ", " = " + str_CompanyId);
                    Log.e("str_CompanyName ", " = " + str_CompanyName);

//                    if (dbhelper.isTitleExist(str_CompanyId)) {
//                        //Upadte
//                        dbhelper.updatedetails(str_CompanyId, str_CompanyName);
//                        Log.e("Data updated in ", "Company Table !!");
//                    } else {
//
//                        //insert
//                        dbhelper.insertCompany(str_CompanyId, str_CompanyName);
//                        Log.e("Data inserted in ", "Company Table !!");
//                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }
    }

注意:

  • 无需使用嵌套的 try 和 catch。
  • 对于“strResult = "Did not work!";”,始终确保每个 异常处理得好(在 jsonstr 的检查点是否为空 无法识别解析输入流时发生的错误可能导致 严重的错误)。
  • HttpClient 已被弃用,因此请确保使用最新版本的任何 第三方库,如 Volley 或 Retrofit 用于网络 操作和 GSON 用于解析响应。它会最小化你的 努力。

谢谢。

【讨论】:

    猜你喜欢
    • 2013-08-24
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多