[英]How To Extract elements from a list of objects and store in String array
我正在嘗試從服務器獲取響應並將其顯示在微調器中。我的服務器返回的響應包含JSON數組。 JSON數組具有兩個字段question和q_id,我想將數據都存儲在不同的字符串數組中,並想用問題的詳細信息填充微調器,並使用從微調器中選擇的問題的索引,我想從q_id數組中獲取元素並發送到服務器
謝謝。
{
"data": [
{
"q_id": "21",
"question": "Flipkart VS Amazone which is better?"
},
{
"q_id": "22",
"question": "Test"
},
],
"status": true,
"message": "Bank Ac created sucessfully"}
public class Question {
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("message")
@Expose
private String message;
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class Datum {
@SerializedName("q_id")
@Expose
private String qId;
@SerializedName("question")
@Expose
private String question;
public String getQId() {
return qId;
}
public void setQId(String qId) {
this.qId = qId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
private void getQuestions() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).addInterceptor(interceptor).build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(CommonObjects.BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
getquestion Service = retrofit.create(getquestion.class);
Call<Question> call = Service.post(id);
call.enqueue(new Callback<Question>() {
@Override
public void onResponse(Call<Question> call, Response<Question> response) {
if (!response.body().getStatus()) {
message = response.body().getMessage();
showMessage(message);
} else {
Question jsonResponse = response.body();
message = response.body().getMessage();
CommonObjects.q.setData(jsonResponse.getData());
CommonObjects.q.setMessage(message);
CommonObjects.q.setStatus(response.body().getStatus());
}
}
@Override
public void onFailure(Call<Question> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2, t.getMessage()));
}
});
}
public interface getquestion {
@FormUrlEncoded
@POST("feedback_question")
Call<Question> post(
@Field("userid") String question
);
}
您需要JSONObject和JSONArray類。
解析來自服務器的JSON響應的步驟。
實例化JSONObject
類,然后將包含JSON
響應的變量傳遞到JSONObject
類的構造函數中。
JSONObject jsonObj = new JSONObject(jsonResponse);
JSONObject
類中可用的方法在JSON
響應中獲取任何內容。 例如,如果您需要data
數組以提取它,則可以使用getJSONArray
方法並傳入該數組的鍵(在您的情況下為data
JSONArray arr = jsonObj.getJSONArray("data");
現在要提取數據數組中的data
,請使用JSONArray
類中可用的適當方法。
同樣,您可以使用JSONObject
和JSONArray
類提取其他數據。
請記住,如果您有JSON
對象,請使用JSONObject
類中可用的方法,如果您有JSON
數組,請使用JSONArray
類中可用的方法來提取所需的數據。
您可以像這樣使用org.json庫提取該JSON:
JSONObject jsonObject = new JSONObject(serverResponse);
JSONArray jsonArray = jsonObject.getJSONArray("data");
ArrayList<String> qids = new ArrayList<>();
ArrayList<String> questions = new ArrayList<>();
for (int i=0; i<jsonArray.length(); i++) {
JSONObject item = jsonArray.get(i);
qids.add(item.getString("q_id"));
questions.add(item.getString("question"));
}
使用此類代碼來解析Json數組。 有關更多示例
JSONObject jsonObj = new JSONObject(jsonResponse);
JSONArray data = jsonObj.getJSONArray("data");
Question question = new Question();
List<Datum> datumList = new ArrayList<Datum>();
if (data != null) {
for (int i = 0; i < data.length(); i++) {
try {
JSONObject obj = (JSONObject) data.get(i);
Datum datum = new Datum();
datum.setQId(data.getString("q_id"));
datum.setQuestion(data.getString("question"));
datumList.add(datum);
} catch (Exception e) {
e.printStackTrace();
}
}
}
question.setData(datumList);
欲了解更多細節和學習
用於每個循環並在arraylist中填充數據。 做這樣的事情。
else {
ArrayList<String> QidList = new ArrayList<String>();
ArrayList<String> QuestionList = new ArrayList<String>();
Question jsonResponse = response.body();
message = response.body().getMessage();
CommonObjects.q.setData(jsonResponse.getData());
CommonObjects.q.setMessage(message);
CommonObjects.q.setStatus(response.body().getStatus());
for (Datum data : jsonResponse.getData()) {
if((!TextUtils.isEmpty(data.getQId())) &&
(!TextUtils.isEmpty(data.getQuestion()))){
QidList.add(data.getQId());
QuestionList.add(data.getQuestion());
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.