【问题标题】:Iterating/Getting JSONObject inside of JSONArray without key在没有键的情况下在 JSONArray 中迭代/获取 JSONObject
【发布时间】:2019-03-25 16:56:18
【问题描述】:

需要获取这个 JSONArray 中的 Every JSONObject。目前只有 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();
                    }

【问题讨论】:

  • 看起来您使用i 作为条件,但在您的周期中永远不要增加或减少它。这将使它成为无限循环,一旦你的数组超出范围,就会抛出异常。
  • 像回答stackoverflow.com/questions/47197887/…的那个人一样创建模型类
  • 你可以使用库

标签: java android json jsonparser


【解决方案1】:
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();
}

这也是一种方法

【讨论】:

    【解决方案2】:

    第 1 步:在库下方集成。

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

    第二步:复制到 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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2013-02-28
      相关资源
      最近更新 更多