亲爱的 friend ,我想从索引中获取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);
最佳答案
试试这个:
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
}
关于java - Android Json数组数据错误并且无法从json数组索引中查找或获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097382/