需要在此JSONArray中获取每个JSONObject。当前只有2个JSONObject,但将来还会更多。因此,我需要根据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();
}
答案 0 :(得分: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();
}
这也是一种实现方法
答案 1 :(得分:1)
步骤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();
}