Итерация / Получение JSONObject внутри JSONArray без ключа - PullRequest
0 голосов
/ 25 марта 2019

Необходимо получить Every JSONObject внутри этого JSONArray.В настоящее время есть только 2 JSONObjects, но в будущем их будет больше.Итак, мне нужно сделать его динамическим в зависимости от длины JSONArray.

Вот весь JSON:

{
  "data": [
    {
      "previous_class_percentage": 58.0,
      "speech_disabilty": true,
      "hearing_difficulty": false,
      "last_name": "Krishnana",
      "weight": 54.0,
      "submitted_timestamp": "2018-02-15T10:22:00Z",
      "id_number": "VS017BH0004"
    },
    {
      "previous_class_percentage": 88.0,
      "speech_disabilty": true,
      "hearing_difficulty": false,
      "last_name": "Krishnana",
      "weight": 54.0,
      "submitted_timestamp": "2018-02-14T10:22:00Z",
      "id_number": "VS017BH0006"
    }
  ]
}

Я пытаюсь что-то вроде этого

  try {
                        int k = 0;
                        while (i<sectionJsonArr.length()){
                            JSONObject data = sectionJsonArr.optJSONObject(k);
                            Log.d("json-array",data+"");
                            String b_certificate_no = data.getString("id_number");
                            if (student_birth_certfct_number.equals(b_certificate_no)){
                                Log.d("text","inside if");
                                //TO-DO CODE
                            }
                            k++;
                        }
                    }catch (JSONException e){
                        e.printStackTrace();
                    }

Ответы [ 2 ]

1 голос
/ 25 марта 2019

Шаг-1: интегрировать под библиотеку.

implementation 'com.google.code.gson:gson:2.8.5'

Шаг-2: копировать ниже класса POJO.

public class Example {

    @SerializedName("data")
    @Expose
    private ArrayList<Datum> data = null;

    public ArrayList<Datum> getData() {
        return data;
    }

    public void setData(ArrayList<Datum> data) {
        this.data = data;
    }

    public class Datum {

        @SerializedName("previous_class_percentage")
        @Expose
        private Double previousClassPercentage;
        @SerializedName("speech_disabilty")
        @Expose
        private Boolean speechDisabilty;
        @SerializedName("hearing_difficulty")
        @Expose
        private Boolean hearingDifficulty;
        @SerializedName("last_name")
        @Expose
        private String lastName;
        @SerializedName("weight")
        @Expose
        private Double weight;
        @SerializedName("submitted_timestamp")
        @Expose
        private String submittedTimestamp;
        @SerializedName("id_number")
        @Expose
        private String idNumber;

        public Double getPreviousClassPercentage() {
            return previousClassPercentage;
        }

        public void setPreviousClassPercentage(Double previousClassPercentage) {
            this.previousClassPercentage = previousClassPercentage;
        }

        public Boolean getSpeechDisabilty() {
            return speechDisabilty;
        }

        public void setSpeechDisabilty(Boolean speechDisabilty) {
            this.speechDisabilty = speechDisabilty;
        }

        public Boolean getHearingDifficulty() {
            return hearingDifficulty;
        }

        public void setHearingDifficulty(Boolean hearingDifficulty) {
            this.hearingDifficulty = hearingDifficulty;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public Double getWeight() {
            return weight;
        }

        public void setWeight(Double weight) {
            this.weight = weight;
        }

        public String getSubmittedTimestamp() {
            return submittedTimestamp;
        }

        public void setSubmittedTimestamp(String submittedTimestamp) {
            this.submittedTimestamp = submittedTimestamp;
        }

        public String getIdNumber() {
            return idNumber;
        }

        public void setIdNumber(String idNumber) {
            this.idNumber = idNumber;
        }

    }

}

Шаг-3: Анализ ответа.

// "response" String that you will get from server or other.

Example example = new Gson().fromJson(response,Example.class);

    for (Example.Datum response: example.getData()) {
        String b_certificate_no = response.getIdNumber();
    }
1 голос
/ 25 марта 2019
try{
    for( int i=0; i < sectionJsonArr.length(); i++){
      JSONObject data = obj.optJSONObject(i);
      Log.d("json-array",data+"");
      String b_certificate_no = data.getString("id_number");
      if (student_birth_certfct_number.equals(b_certificate_no)){
         Log.d("text","inside if");
             //TO-DO CODE
      }
    }
}catch(JSONException e){
  e.printStackTrace();
}

это также способ сделать это

...