【问题标题】:Android Json array data Error and unable to find or get data from json array indexAndroid Json 数组数据错误,无法从 json 数组索引中找到或获取数据
【发布时间】:2019-09-25 11:30:46
【问题描述】:

亲爱的朋友,我想从索引中获取 json 数组数据的值并与我的商店 ID 进行比较。 例如:

我想从 json 数组中获取 InstituteID 并与值“3”进行比较,如果为真,则在 textview 中显示存储的主题值 if (instituteID == 3){tv.setText(subject);} InstituteID 和主题是也来自 json。告诉我我哪里错了

ArrayList<HashMap<String,String>> arraylist = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView)findViewById(R.id.subject1);
        tv2 = (TextView)findViewById(R.id.subject2);
        tv3 = (TextView)findViewById(R.id.subject3);
        tv4 = (TextView)findViewById(R.id.subject4);
        tv5 = (TextView)findViewById(R.id.subject5);
        tv6 = (TextView)findViewById(R.id.subject6);

//here calling json asynctask
new getTacherJson().execute();
        DisplayData();
}

public void DisplayData(){
        SharedPreferences preferences = getSharedPreferences("Email's response",MODE_PRIVATE);
        shared_pref_emal = preferences.getString("response",null);
        Log.e("Email's response",shared_pref_emal);

        SharedPreferences preferences1 = getSharedPreferences("Institute's ID",MODE_PRIVATE);
        shared_pref_ins_id = preferences1.getString("response id",null);
        Log.e("inst. id",shared_pref_ins_id);

        if (institute_ID == shared_pref_ins_id){
            tv1.setText(subject);
            tv2.setText(subject2);
            tv3.setText(subject3);
            tv4.setVisibility(View.GONE);
            tv5.setVisibility(View.GONE);
            tv6.setVisibility(View.GONE);
        }
        else {
            tv1.setText("Nothing");
        }
    }

//here is my Json Data
{"response":[{"id":"11","class":"One Class","section":"Morning","teacher":"abc@gmail.com","subject":"Urdu","fromTime":"08:00","toTime":"08:30","instituteID":"3","created_at":"2019-09-12 04:39:31"},{"id":"12","class":"One Class","section":"Morning","teacher":"abc@gmail.com","subject":"Pakistan Studies","fromTime":"08:30","toTime":"09:00","instituteID":"3","created_at":"2019-09-12 04:39:49"},{"id":"13","class":"BSSE","section":"Evening","teacher":"abc@gmail.com","subject":"Introduction to CMP","fromTime":"15:00","toTime":"17:30","instituteID":"3","created_at":"2019-09-16 07:39:43"}]}


MyHttpHandler myhttp = new MyHttpHandler();
            String teacherURL = url+"/showTeacherCourses/"+get_email+"/"+shared_pref_ins_id;
            String teachDash = myhttp.MyServiceCall(teacherURL);
            Log.e(TAG,"Response From Teacher Courses"+teachDash);

        if (teachDash != null) {
            try {
                JSONObject jsonObject = new JSONObject(teachDash);
                JSONArray teachArray = jsonObject.getJSONArray("response");
                Log.e("Array length", String.valueOf(teachArray.length()));

                // looping through All Contacts
                for (int i = 0; i < teachArray.length(); i++) {
                    JSONObject c = teachArray.getJSONObject(i);

                    id1 = c.getString("id");
                    clasx = c.getString("class");
                    section = c.getString("section");
                    teacher_emaill = c.getString("teacher");
                    subject = c.getString("subject");
                    from_time = c.getString("fromTime");
                    to_time = c.getString("toTime");
                    institute_ID = c.getString("instituteID");
                    created_at = c.getString("created_at");


HashMap<String, String> tech_dta_ftch = new HashMap<>();

                        // adding each child node to HashMap key => value

                        tech_dta_ftch.put("id", id1);
                        tech_dta_ftch.put("class",clasx);
                        tech_dta_ftch.put("section",section);
                        tech_dta_ftch.put("teacher",teacher_emaill);
                        tech_dta_ftch.put("subject",subject);
                        tech_dta_ftch.put("fromTime",from_time);
                        tech_dta_ftch.put("toTime",to_time);
                        tech_dta_ftch.put("created_at", created_at);
                        tech_dta_ftch.put("instituteID", institute_ID);

                        // adding contact to contact list
                        arraylist.add(tech_dta_ftch);

【问题讨论】:

  • 在此处发布您的完整 json 响应
  • 使用 put() 方法时基于文档:如果映射先前包含键的映射,则替换旧值。 more info

标签: java android json android-studio


【解决方案1】:

试试这个:

for(int i = 0; i < teachArray.length(); i++){
    JSONObject c = teachArray.getJSONObject(i);
    HashMap<String, String> tech_dta_ftch = new HashMap<>();
    tech_dta_ftch.put("id", c.getString("id"));
    tech_dta_ftch.put("class",c.getString("class"));
    tech_dta_ftch.put("section",c.getString("section"));
    tech_dta_ftch.put("teacher",c.getString("teacher"));
    tech_dta_ftch.put("subject",c.getString("subject"));
    tech_dta_ftch.put("fromTime",c.getString("fromTime"));
    tech_dta_ftch.put("toTime",c.getString("toTime"));
    tech_dta_ftch.put("created_at", c.getString("created_at"));
    tech_dta_ftch.put("instituteID", c.getString("instituteID"));
    arraylist.add(tech_dta_ftch);   
}

为了比较当前我在第一个索引处选择的数组列表的特定索引处的机构 ID,您可以按照自己的方式使用它:

if (arraylist.get(0).get("instituteID").equalsIgnoreCase("ID_TO_CHECK_WITH")){
    //DO YOUR STUFF
}

【讨论】:

  • 当我输入此代码并运行时,我的活动因此代码而关闭 if (arraylist.get(0).get("instituteID").equalsIgnoreCase("ID_TO_CHECK_WITH")){ //DO你的东西}
  • 你在初始化你的arraylist吗?你也可以分享关闭你的应用程序的代码,如果项目被添加到列表中,检索这些项目时应该没有问题
  • 您需要在将项目添加到 arraylist 时放置一个断点,即 arraylist.add(tech_dta_ftch) 并检查 hashmap 的内容并记录数组大小。
  • 您确定从服务器获取数据后会发生此异常吗?一个原因可能是您试图在添加数据之前从空数组列表中获取元素。您可以在您的代码中放置断点,从服务器获得响应,并检查您收到的数据并逐行运行调试器,以确保您的哈希图已添加到数组列表中,developer.android.com/studio/debug 您可以看看如何调试您的应用程序。只需确保您在服务器响应后尝试从 arraylist 获取值。
  • 您正试图从列表中获取数据,然后再获得它。正确的做法是将 displayData 放在 onPostExecute 中,以便在使用前填充您的列表。如果它对您有用(确实如此),请点赞并接受。
猜你喜欢
  • 2023-03-21
  • 2015-02-02
  • 2022-12-06
  • 2020-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多