【发布时间】:2021-10-25 05:31:14
【问题描述】:
如何仅获取“字段”数组下每个对象的“名称”字符串,主数组索引为 0,然后使用循环或其他超级想法获取下一个索引
[
{
"name": "Bank1",
"fields": [
{
"name": "Email",
"slug": "email",
"type": "input"
},
{
"name": "City",
"slug": "city",
"type": "input"
},
{
"name": "Screenshot",
"slug": "screenshot",
"type": "file"
},
{
"name": "Full Name",
"slug": "full-name",
"type": "input"
}
],
"status": "Active"
},
{
"name": "Bank2",
"fields": [
{
"name": "Email",
"slug": "email",
"type": "input"
},
{
"name": "City",
"slug": "city",
"type": "input"
},
{
"name": "Screenshot",
"slug": "screenshot",
"type": "file"
},
{
"name": "Submitted Date",
"slug": "submitted-date",
"type": "calendar"
}
],
"status": "Active"
}
]
我想要的输出:
电子邮件
城市
截图
全名
表示在输出中,我有索引 0,第一个对象数组数据...
我还做了什么
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String p_name = jsonObject.getString("name");
ArrayList<String> arr = new ArrayList<>();
JSONArray ja = jsonObject.getJSONArray("fields");
int len = ja.length();
for (int j = 0; j < len; j++) {
JSONObject json = ja.getJSONObject(j);
arr.add(json.getString("name"));
}
}}catch block...
这给了我所有索引名称数据,我只想要特定的索引数据
我的当前输出:
电子邮件
城市
截图
全名
电子邮件
城市
截图
提交日期
【问题讨论】:
-
使用 Hash map 存储索引及其值 name
-
你怎么给我一个例子? @Piyush
标签: java android arrays json nested