org.json.jsonexception index 3超出范围0..3)

时间:2014-12-12 05:10:54

标签: android json arrays

我正在创建一个Android应用程序,我正在使用json解析,我试图从服务器得到响应我的响应在下面,我不知道如何使用单个键访问多个值可以任何人帮助我吗?< / p>

{
  "filters_id":53,"user_login_id":2650, 
  "allow":"N",
  "mothertongue":"Bhojpuri",
  "maritalstatus":
    [
      "Widow\/Widower","Divorcee","Separated"
    ],
  "min_age":19,
  "max_age":22,
  "country":"India"
}

protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
        String jsonStr = sh.makeServiceCall(FILTER_URL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);


            try {
                jsonObj = new JSONObject(jsonStr);


                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(FILTER_ID, jsonObj.getString(FILTER_ID));
                    map.put(FILTER_USER_LOGIN_ID,jsonObj.getString(FILTER_USER_LOGIN_ID));
                    map.put(FILTER_ALLOW, jsonObj.getString(FILTER_ALLOW));
                    map.put(FILTER_MOTHERTONGUE, jsonObj.getString(FILTER_MOTHERTONGUE));
                    map.put(FILTER_MARITAL, jsonObj.getString(FILTER_MARITAL));
                    map.put(FILTER_MINAGE, jsonObj.getString(FILTER_MINAGE));
                    map.put(FILTER_MAXAGE, jsonObj.getString(FILTER_MAXAGE));
                    map.put(FILTER_COUNTRY, jsonObj.getString(FILTER_COUNTRY));

                    /*JSONArray status = jsonObj.getString("maritalstatus");
                    status.getString(0);*/
                    final String radiovalue = jsonObj.getString("allow");
                    Log.d("Value: ", "> " + radiovalue);
                    final String mothertong = jsonObj.getString("mothertongue");
                    Log.d("Value: ", "> " + mothertong);


                   /* final String marital = jsonObj.getJSONArray("maritalstatus");
                    Log.d("Value: ", "> " + marital);*/


                    /*JSONArray maritalarray = (JSONArray) jsonObj.get("maritalstatus");
                    final String marital=maritalarray.getString(0);
                    Log.d("Value: ", "> " + marital);*/


                    JSONArray msg = (JSONArray) jsonObj.get(FILTER_MARITAL);
                    for(int i=0;i< FILTER_MARITAL.length();i++)
                    {
                         marital= msg.getString(i);

                    }
                    Log.d("Value: ", "> " + marital);
                    /*Iterator<String> iterator = msg.iterator();
                    while (iterator.hasNext()) {
                        System.out.println(iterator.next());
                    }*/
                    final String minimumage = jsonObj.getString("min_age");
                    Log.d("Value: ", "> " + minimumage);
                    final String maximumage = jsonObj.getString("max_age");
                    Log.d("Value: ", "> " + maximumage);
                    final String countrys = jsonObj.getString("country");
                    Log.d("Value: ", "> " + countrys);

                    getActivity().runOnUiThread(new  Runnable() 
                 {
                    @Override
                    public void run() 
                    {
                        Intent intent=new Intent(getActivity(),Filter.class);
                        intent.putExtra("id", id);
                        intent.putExtra("radiostatus", radiovalue);
                        intent.putExtra("motherstong", mothertong);
                        intent.putExtra("marriagestatus", marital);
                        intent.putExtra("minages", minimumage);
                        intent.putExtra("maxages", maximumage);
                        intent.putExtra("countries", countrys);
                        startActivity(intent);
                    } 
                });





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


        return data;
    }


    protected void onPostExecute(ArrayList<HashMap<String,String>> result) {

        super.onPostExecute(result);


        // dismiss the dialog after getting all albums
        if (pDialog.isShowing())
            pDialog.dismiss();
        // updating UI from Background Thread
        /*Intent mIntent = new Intent(getActivity(),Filter.class);
        Bundle extras = mIntent.getExtras();
        try {
            extras.putString("radiostatus", jsonObj.getString(FILTER_ALLOW));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  */


    }

4 个答案:

答案 0 :(得分:1)

我认为您的问题是在访问FILTER_MARITAL.length()时使用msg的循环中。

这会将计数值计算为字符串maritalstatus的长度,该字符串长度超过数组长度,因此会导致out of range执行。

修复如下:

JSONArray msg = (JSONArray) jsonObj.get(FILTER_MARITAL);
for(int i=0;i< msg.length();i++)
{
   marital= msg.getString(i);

}

答案 1 :(得分:1)

marital = "";

    JSONArray msg = (JSONArray) jsonObj.get(FILTER_MARITAL);
    for(int i=0;i< msg.length();i++)
    {
       marital+= msg.getString(i);

    }

答案 2 :(得分:0)

你的json是错误的json数组&#34; maritalstatus&#34;缺少&#34;:&#34;

correct your json response 
  {
     "filters_id":53,"user_login_id":2650, 
     "allow":"N",
     "mothertongue":"Bhojpuri",
     "maritalstatus":
     [
      "Widow\/Widower","Divorcee","Separated"
     ],
   "min_age":19,
   "max_age":22,
   "country":"India"
  }

答案 3 :(得分:0)

JSONArray result = jsonResponse.getJSONArray("results");
final int numberofitems = result.length();
for (int i = 0; i < numberofitems; i++){
    JSONObject perResult = result.getJSONObject(i);
}