Android:如何从对象名称可能更改的 JSON 响应中检索多个 JSON 对象?
Android: How to retrieve multiple JSON objects from a JSON response in which object names might change?
我有以下 JSON 响应片段,用于按 first_name
、last_name
和 email
.
过滤列表视图
"data": {
"filters": {
"first_name": {
"key": "first_name",
"label": "First Name",
"order": 1,
"values": [
"Shaun",
"Max",
"Tyler"
],
"filter_type": "select"
},
"last_name": {
"key": "last_name",
"label": "Last Name",
"order": 2,
"values": [
"Nash",
"Mally",
"Carick"
],
"filter_type": "select"
},
"email": {
"key": "email",
"label": "Email",
"order": 3,
"values": [
"shaun@email.com",
"max@email.com",
"tyler@email.com"
],
"filter_type": "select"
}
},
...
}
我的问题是 first_name
、last_name
和 email
是自定义过滤器,这意味着这些 JSON 对象名称可以更改。例如,另一个响应可能如下所示:
"data": {
"filters": {
"age": {
"key": "age",
"label": "Age",
"order": 1,
"values": [
"33",
"24",
"47"
],
"filter_type": "select"
},
"middle_name": {
"key": "middle_name",
"label": "Middle Name",
"order": 2,
"values": [
"Nicholas",
"Ava",
"George"
],
"filter_type": "select"
},
"email": {
"key": "email",
"label": "Email",
"order": 3,
"values": [
"shaun@email.com",
"max@email.com",
"tyler@email.com"
],
"filter_type": "select"
}
},
...
}
这里使用的自定义过滤器是 age
、middle_name
和 email
。虽然这些过滤器名称可能不同,但每个过滤器始终有一个 key
、label
、order
、values
和 filter_type
字段。
我无法理解如何使用 GSON 正确解析此处的信息。我尝试研究使用 JSON 到 POJO 网站,但我不知道如何应用它,因为过滤器并不总是相同的。我也在尝试这样做:
JSONObject dataObject = myJSON.getJSONObject("data");
if(dataObject.has("filters")){
JSONObject filterJSONObject = dataObject.getJSONObject("filters");
//I need to retrieve the keys/values for each filter here
}
但这只是 returns 第一个过滤器对象而不是其余的。
如有任何帮助,我们将不胜感激
您可以像下面这样查看
JSONObject firstNameFilter = filterJSONObject.getJSONObject("first_name");
JSONObject ageFilter = filterJSONObject.getJSONObject("age");
if(firstNameFilter != null){
}
if(ageFilter != null){
}
我建议采用不同的方法。您可以像这样创建 类(名称仅供示例,但字段名称很重要):
public static class Holder {
private final Filters data;
public Holder(Filters data) {
this.data = data;
}
public Filters getData() {
return data;
}
}
public static class Filters {
private final Map<String, Value> filters;
public Filters(Map<String, Value> filters) {
this.filters = filters;
}
public Map<String, Value> getFilters() {
return filters;
}
}
public static class Value {
private final String label;
private final int order;
public Value(String label, int order) {
this.label = label;
this.order = order;
}
public String getLabel() {
return label;
}
public int getOrder() {
return order;
}
}
以上结构将序列化到您的示例中 json:
Value value1 = new Value("label1", 1);
Value value2 = new Value("label2", 2);
Map<String, Value> data = new HashMap<>();
data.put("age", value1);
data.put("email", value2);
Filters filters = new Filters(data);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Log.d("json", gson.toJson(new Holder(filters)));
这是日志输出:
{
"data": {
"filters": {
"age": {
"label": "label1",
"order": 1
},
"email": {
"label": "label2",
"order": 2
}
}
}
}
您可以使用 keys()
方法
获得包含 JSONObject 中键列表的迭代器
你可以试试这个方法
Map<String, Object> filterMap = new HashMap<String, Object>();
if(dataObject.has("filters")){
JSONObject filterJSONObject = null;
try {
filterJSONObject = dataObject.getJSONObject("filters");
} catch (JSONException e) {
e.printStackTrace();
}
//I need to retrieve the keys/values for each filter here
Iterator<String> keysItr = filterJSONObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = null;
try {
value = filterJSONObject.get(key);
} catch (JSONException e) {
e.printStackTrace();
}
if(value instanceof JSONObject) {
value = togetValueMap((JSONObject) value);
}
filterMap.put(key, value);
}
}
private ArrayList<FilterValue> togetValueMap(JSONObject value) {
ArrayList<FilterValue> arrayList = new ArrayList<FilterValue>();
List<String> list = new ArrayList<String>();
for (int i = 0; i < value.length(); i++) {
FilterValue filterValue = new FilterValue();
try {
filterValue.setKey(value.getString("key"));
filterValue.setLabel(value.getString("label"));
filterValue.setOrder(value.getInt("order"));
filterValue.setFilter_type(value.getString("filter_type"));
JSONArray jsonArray = value.getJSONArray("values");
for (int j=0; j<jsonArray.length(); j++) {
list.add(jsonArray.getString(j) );
}
filterValue.setValues(list);
arrayList.add(filterValue);
} catch (JSONException e) {
e.printStackTrace();
}
}
return arrayList;
}
public static class FilterValue {
private String key;
private String label;
private int order;
private List<String> values;
private String filter_type;
public void setKey(String key) {
this.key = key;
}
public void setLabel(String label) {
this.label = label;
}
public void setOrder(int order) {
this.order = order;
}
public void setValues(List<String> values) {
this.values = values;
}
public void setFilter_type(String filter_type) {
this.filter_type = filter_type;
}
public String getKey() {
return key;
}
public String getLabel() {
return label;
}
public int getOrder() {
return order;
}
public List<String> getValues() {
return values;
}
public String getFilter_type() {
return filter_type;
}
}
我有以下 JSON 响应片段,用于按 first_name
、last_name
和 email
.
"data": {
"filters": {
"first_name": {
"key": "first_name",
"label": "First Name",
"order": 1,
"values": [
"Shaun",
"Max",
"Tyler"
],
"filter_type": "select"
},
"last_name": {
"key": "last_name",
"label": "Last Name",
"order": 2,
"values": [
"Nash",
"Mally",
"Carick"
],
"filter_type": "select"
},
"email": {
"key": "email",
"label": "Email",
"order": 3,
"values": [
"shaun@email.com",
"max@email.com",
"tyler@email.com"
],
"filter_type": "select"
}
},
...
}
我的问题是 first_name
、last_name
和 email
是自定义过滤器,这意味着这些 JSON 对象名称可以更改。例如,另一个响应可能如下所示:
"data": {
"filters": {
"age": {
"key": "age",
"label": "Age",
"order": 1,
"values": [
"33",
"24",
"47"
],
"filter_type": "select"
},
"middle_name": {
"key": "middle_name",
"label": "Middle Name",
"order": 2,
"values": [
"Nicholas",
"Ava",
"George"
],
"filter_type": "select"
},
"email": {
"key": "email",
"label": "Email",
"order": 3,
"values": [
"shaun@email.com",
"max@email.com",
"tyler@email.com"
],
"filter_type": "select"
}
},
...
}
这里使用的自定义过滤器是 age
、middle_name
和 email
。虽然这些过滤器名称可能不同,但每个过滤器始终有一个 key
、label
、order
、values
和 filter_type
字段。
我无法理解如何使用 GSON 正确解析此处的信息。我尝试研究使用 JSON 到 POJO 网站,但我不知道如何应用它,因为过滤器并不总是相同的。我也在尝试这样做:
JSONObject dataObject = myJSON.getJSONObject("data");
if(dataObject.has("filters")){
JSONObject filterJSONObject = dataObject.getJSONObject("filters");
//I need to retrieve the keys/values for each filter here
}
但这只是 returns 第一个过滤器对象而不是其余的。
如有任何帮助,我们将不胜感激
您可以像下面这样查看
JSONObject firstNameFilter = filterJSONObject.getJSONObject("first_name");
JSONObject ageFilter = filterJSONObject.getJSONObject("age");
if(firstNameFilter != null){
}
if(ageFilter != null){
}
我建议采用不同的方法。您可以像这样创建 类(名称仅供示例,但字段名称很重要):
public static class Holder {
private final Filters data;
public Holder(Filters data) {
this.data = data;
}
public Filters getData() {
return data;
}
}
public static class Filters {
private final Map<String, Value> filters;
public Filters(Map<String, Value> filters) {
this.filters = filters;
}
public Map<String, Value> getFilters() {
return filters;
}
}
public static class Value {
private final String label;
private final int order;
public Value(String label, int order) {
this.label = label;
this.order = order;
}
public String getLabel() {
return label;
}
public int getOrder() {
return order;
}
}
以上结构将序列化到您的示例中 json:
Value value1 = new Value("label1", 1);
Value value2 = new Value("label2", 2);
Map<String, Value> data = new HashMap<>();
data.put("age", value1);
data.put("email", value2);
Filters filters = new Filters(data);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Log.d("json", gson.toJson(new Holder(filters)));
这是日志输出:
{
"data": {
"filters": {
"age": {
"label": "label1",
"order": 1
},
"email": {
"label": "label2",
"order": 2
}
}
}
}
您可以使用 keys()
方法
你可以试试这个方法
Map<String, Object> filterMap = new HashMap<String, Object>();
if(dataObject.has("filters")){
JSONObject filterJSONObject = null;
try {
filterJSONObject = dataObject.getJSONObject("filters");
} catch (JSONException e) {
e.printStackTrace();
}
//I need to retrieve the keys/values for each filter here
Iterator<String> keysItr = filterJSONObject.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = null;
try {
value = filterJSONObject.get(key);
} catch (JSONException e) {
e.printStackTrace();
}
if(value instanceof JSONObject) {
value = togetValueMap((JSONObject) value);
}
filterMap.put(key, value);
}
}
private ArrayList<FilterValue> togetValueMap(JSONObject value) {
ArrayList<FilterValue> arrayList = new ArrayList<FilterValue>();
List<String> list = new ArrayList<String>();
for (int i = 0; i < value.length(); i++) {
FilterValue filterValue = new FilterValue();
try {
filterValue.setKey(value.getString("key"));
filterValue.setLabel(value.getString("label"));
filterValue.setOrder(value.getInt("order"));
filterValue.setFilter_type(value.getString("filter_type"));
JSONArray jsonArray = value.getJSONArray("values");
for (int j=0; j<jsonArray.length(); j++) {
list.add(jsonArray.getString(j) );
}
filterValue.setValues(list);
arrayList.add(filterValue);
} catch (JSONException e) {
e.printStackTrace();
}
}
return arrayList;
}
public static class FilterValue {
private String key;
private String label;
private int order;
private List<String> values;
private String filter_type;
public void setKey(String key) {
this.key = key;
}
public void setLabel(String label) {
this.label = label;
}
public void setOrder(int order) {
this.order = order;
}
public void setValues(List<String> values) {
this.values = values;
}
public void setFilter_type(String filter_type) {
this.filter_type = filter_type;
}
public String getKey() {
return key;
}
public String getLabel() {
return label;
}
public int getOrder() {
return order;
}
public List<String> getValues() {
return values;
}
public String getFilter_type() {
return filter_type;
}
}