내 모델에 응답을 저장하려고하는데 다른 배열 안에있는 경우 어떻게 값을 얻을 수 있습니까? 제가받은 반응이 좀 혼란 스럽습니다. 이것은 응답의 모습입니다.
{
"data": [
{
"id": "1",
"directory_name": "Directory 1",
"details": [
{
"sub_directory_name": "Sub Directory 1",
"address": "sub",
"email": [
{
"key": 0,
"value": "123"
}
],
"contact_no": [
{
"key": 0,
"value": "123"
}
],
"description": ""
},
{
"sub_directory_name": "Sub Directory 2",
"address": "National",
"email": [
{
"key": 0,
"value": "123"
}
],
"contact_no": [
{
"key": 0,
"value": "123"
}
],
"description": ""
},
{
"sub_directory_name": "Sub Directory 3",
"address": "Syb",
"email": [
{
"key": 0,
"value": "123"
},
{
"key": 1,
"value": "123"
}
],
"contact_no": [
{
"key": 0,
"value": "123"
},
{
"key": 1,
"value": "123"
}
],
"description": ""
},
{
"sub_directory_name": "Sub Directory 4",
"address": "Sub",
"email": [
{
"key": 0,
"value": "123"
},
{
"key": 1,
"value": "123"
},
{
"key": 2,
"value": "(123"
}
],
"contact_no": [
{
"key": 0,
"value": "123"
},
{
"key": 1,
"value": "123"
},
{
"key": 2,
"value": "123"
}
],
"description": ""
}
]
},
{
"id": "2",
"directory_name": "Directory 2",
"details": [
{
"sub_directory_name": "Sub Directory 2",
"address": "Sub",
"email": [
{
"key": 0,
"value": "test"
}
],
"contact_no": [
{
"key": 0,
"value": "test"
},
{
"key": 1,
"value": "test"
}
],
"description": ""
},
{
"sub_directory_name": "Sub Directory 3",
"address": "test",
"email": [
{
"key": 0,
"value": "tset"
}
],
"contact_no": [
{
"key": 0,
"value": "test"
},
{
"key": 1,
"value": "test"
},
{
"key": 2,
"value": "test"
}
],
"description": ""
},
{
"sub_directory_name": "Sub Directory 3",
"address": "test",
"email": [
{
"key": 0,
"value": "test"
}
],
"contact_no": [
{
"key": 0,
"value": "test"
}
],
"description": ""
}
]
}
]
}
정말 복잡해 보입니다. 내가 얻을 필요 email
하고 contact_no
받는details
이것은 내가 현재 가지고있는 코드입니다.
발리를 사용하고 있습니다.
CustomRequest stringRequest = new CustomRequest(Request.Method.POST, WebConfig.directories, null,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try {
// Get the object data array from response
JSONArray dataArray = response.getJSONArray("data");
for (int data = 0; data < dataArray.length(); data++) {
// Get the data object and save the common id and directory name for each of the details
JSONObject dataObject = dataArray.getJSONObject(data);
JSONArray detailsArray = dataObject.getJSONArray("details");
Integer ID = dataObject.getInt("id");
String DirName = dataObject.getString("directory_name");
for (int j = 0;j <detailsArray.length();j++) {
DirectoryModel details = new DirectoryModel();
details.setId(ID);
details.setDirectory_name(DirName);
JSONObject detailObject = detailsArray.getJSONObject(j);
details.setSub_directory_name(detailObject.getString("sub_directory_name"));
JSONArray emailsArray = detailObject.getJSONArray("email");
ArrayList<Email> emailArrayList = new ArrayList<>();
for (int k = 0; k <emailsArray.length();k++){
Email email = new Email();
JSONObject emailObject = emailsArray.getJSONObject(k);
email.setKey(emailObject.getString("key"));
email.setValue(emailObject.getString("value"));
details.setEmailArrayList(emailArrayList);
}
JSONArray contactArray = detailObject.getJSONArray("contact_no");
ArrayList<ContactNo> contactNoArrayList = new ArrayList<>();
for (int m = 0; m <contactArray.length();m++){
ContactNo contactNo = new ContactNo();
JSONObject mobileObject = emailsArray.getJSONObject(m);
contactNo.setKey(mobileObject.getString("key"));
contactNo.setValue(mobileObject.getString("value"));
details.setContactNoArrayList(contactNoArrayList);
}
directoryList.add(details);
}
adapter.notifyDataSetChanged();
directoryRecyclerView.setVisibility(View.VISIBLE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
내 코드가 여전히 유효하지 않다는 것을 알고 있으며 내 코드를 통해 내가 가진 문제를 어떻게 구현할 수 있습니까? 어떤 도움이라도 정말 감사하겠습니다.
먼저 세부 정보를 포함하는 모델 또는 pojo 클래스 만들기
Details.class // model 또는 pojo 클래스
public class Details {
String sub_directory_name;
String address;
ArrayList<Email> emailArrayList;
ArrayList<ContactNo> contactNoArrayList;
public String getSub_directory_name() {
return sub_directory_name;
}
public void setSub_directory_name(String sub_directory_name) {
this.sub_directory_name = sub_directory_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public ArrayList<Email> getEmailArrayList() {
return emailArrayList;
}
public void setEmailArrayList(ArrayList<Email> emailArrayList) {
this.emailArrayList = emailArrayList;
}
public ArrayList<ContactNo> getContactNoArrayList() {
return contactNoArrayList;
}
public void setContactNoArrayList(ArrayList<ContactNo> contactNoArrayList) {
this.contactNoArrayList = contactNoArrayList;
}
}
-위의 모델 또는 pojo 클래스에서 ContactNo 및 Email의 또 다른 두 모델 또는 pojo 클래스를 선언해야합니다.
Email.class // 이메일 모델 또는 pojo 클래스
public class Email {
String key;
String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
ContactNo.class // contactno 모델 또는 pojo 클래스
public class ContactNo {
String key;
String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
그런 다음 API를 호출 한 다음 먼저 Details를 포함하는 첫 번째 arraylist를 선언 한 다음 데이터를 저장해야하는 for 루프에서 for 루프를 호출합니다.
ArrayList<Details> detailsArrayList = new ArrayList<>();
JsonArray jdetaul = response.body().get("data").getAsJsonArray();
for (int i = 0; i < jdetaul.size(); i++) {
JsonObject object = jdetaul.get(i).getAsJsonObject();
JsonArray jdetails = object.get("details").getAsJsonArray();
for (int j = 0;j <jdetails.size();j++){
JsonObject detailobj = jdetails.get(j).getAsJsonObject();
Details details = new Details();
details.setAddress(detailobj.get("address").getAsString());
details.setSub_directory_name(detailobj.get("sub_directory").getAsString());
JsonArray jemail = detailobj.get("email").getAsJsonArray();
ArrayList<Email> emailArrayList = new ArrayList<>();
for (int k = 0; k <jemail.size();k++){
JsonObject emailobject = jemail.get(k).getAsJsonObject();
Email email = new Email();
email.setKey(emailobject.get("key").getAsString());
email.setValue(emailobject.get("value").getAsString());
details.setEmailArrayList(emailArrayList);
}
JsonArray jcontact_noarray = detailobj.get("contact_no").getAsJsonArray();
ArrayList<ContactNo> contactNoArrayList = new ArrayList<>();
for (int k = 0; k <jcontact_noarray.size();k++){
JsonObject contactobj = jcontact_noarray.get(k).getAsJsonObject();
ContactNo contactNo = new ContactNo();
contactNo.setKey(contactobj.get("key").getAsString());
contactNo.setValue(contactobj.get("value").getAsString());
details.setContactNoArrayList(contactNoArrayList);
}
detailsArrayList.add(details);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다