gpt4 book ai didi

java - 如何获取并将其存储到Android中数组中的数组变量?

转载 作者:行者123 更新时间:2023-12-01 18:22:40 26 4
gpt4 key购买 nike

我正在尝试将响应存储在我的模型中,但是如果它在另一个数组中,我该如何获取该值?我的 react 让我有点困惑。这是响应的外观。

{
"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": ""
}
]
}
]
}

它看起来真的很复杂。我需要得到 emailcontact_nodetails
这是我目前拥有的代码。

我正在使用截击。
      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 //模型或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 类中,您需要声明另外两个模型或 pojo 类,即 ContactNo 和 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;
    }
    }

    联系电话类(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,然后首先声明第一个包含详细信息的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);
    }

    }
  • 关于java - 如何获取并将其存储到Android中数组中的数组变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277906/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com