org.json.jsonexception指数3超出范围0..3) [英] org.json.jsonexception index 3 out of range 0..3)

查看:993
本文介绍了org.json.jsonexception指数3超出范围0..3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作一个Android应用程序,我使用JSON解析,我试图从服务器响应我的回应是低于,我不知道如何访问与单个键多个值任何一个可以帮助我?

  {
  filters_id:53,user_login_id:2650,
  允许:N,
  母亲的话语:博杰普尔,
  婚姻状况:
    [
      寡妇\\ /鳏夫,离婚,隔离
    ]
  MIN_AGE:19,
  MAX_AGE:22,
  国:印度
}保护的ArrayList<&HashMap的LT;字符串,字符串>> doInBackground(字符串参数... args){
        SH的ServiceHandler =新的ServiceHandler();        //制作到URL的请求和响应得到
        ArrayList的<&HashMap的LT;字符串,字符串>>数据=新的ArrayList<&HashMap的LT;字符串,字符串>>();
        字符串jsonStr = sh.makeServiceCall(FILTER_URL,ServiceHandler.GET);        Log.d(回应:,>中+ jsonStr);
            尝试{
                jsonObj =新的JSONObject(jsonStr);
                    //创建新的HashMap
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                    //将每个子节点的HashMap键=>值
                    map.put(过滤器_id,jsonObj.getString(过滤器_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状态= jsonObj.getString(maritalstatus);
                    status.getString(0); * /
                    最后弦乐radiovalue = jsonObj.getString(允许);
                    Log.d(值,>中+ radiovalue);
                    最后弦乐mothertong = jsonObj.getString(母亲的话语);
                    Log.d(值,>中+ mothertong);
                   / *最后弦乐婚姻= jsonObj.getJSONArray(maritalstatus);
                    Log.d(值,>中+婚姻); * /
                    / * JSONArray maritalarray =(JSONArray),其中jsonObj.get(maritalstatus);
                    最后弦乐婚姻= maritalarray.getString(0);
                    Log.d(值,>中+婚姻); * /
                    JSONArray味精=(JSONArray),其中jsonObj.get(FILTER_MARITAL);
                    的for(int i = 0; I< FILTER_MARITAL.length();我++)
                    {
                         婚姻= msg.getString(I)                    }
                    Log.d(值,>中+婚姻);
                    / *迭代器<串GT;迭代= msg.iterator();
                    而(iterator.hasNext()){
                        的System.out.println(iterator.next());
                    } * /
                    最后弦乐minimumage = jsonObj.getString(MIN_AGE);
                    Log.d(值,>中+ minimumage);
                    最后弦乐maximumage = jsonObj.getString(MAX_AGE);
                    Log.d(值,>中+ maximumage);
                    最后弦乐全国= jsonObj.getString(国家);
                    Log.d(值,>中+国);                    getActivity()。runOnUiThread(新的Runnable()
                 {
                    @覆盖
                    公共无效的run()
                    {
                        意向意图=新意图(getActivity(),Filter.class);
                        intent.putExtra(ID,身份证);
                        intent.putExtra(radiostatus,radiovalue);
                        intent.putExtra(motherstong,mothertong);
                        intent.putExtra(marriagestatus,婚姻);
                        intent.putExtra(minages,minimumage);
                        intent.putExtra(maxages,maximumage);
                        intent.putExtra(国家,国家的);
                        startActivity(意向);
                    }
                });            }赶上(JSONException E){
                e.printStackTrace();
            }
        返回的数据;
    }
    保护无效onPostExecute(ArrayList的<&HashMap的LT;字符串,字符串>>的结果){        super.onPostExecute(结果);
        //让所有专辑后,关闭该对话框
        如果(pDialog.isShowing())
            pDialog.dismiss();
        //从后台线程更新界面
        / *意图mIntent =新意图(getActivity(),Filter.class);
        捆绑额外= mIntent.getExtras();
        尝试{
            extras.putString(radiostatus,jsonObj.getString(FILTER_ALLOW));
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        } * /
    }


解决方案

 婚姻=;    JSONArray味精=(JSONArray),其中jsonObj.get(FILTER_MARITAL);
    的for(int i = 0; I< msg.length();我++)
    {
       婚姻+ = msg.getString(I)    }

I am creating one android app and I am using json parsing,I am trying to get response from server my response is below and I do not know how to access multiple values with single key can any one help me with this?

{
  "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();
        }  */


    }

解决方案

marital = "";

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

    }

这篇关于org.json.jsonexception指数3超出范围0..3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆