我有我想用於我的大學項目的移動應用程序的這個JSON文件,但是我遇到了訪問它的「字段」部分的問題,我不確定訪問的最佳方式它。如何在JSONArray中訪問JSONObject中顯示的JSONObject? Java/Android
{"total_hits":47287,"max_score":11.854574,
"hits":[
{"_index":"f762ef22-e660-434f-9071-a10ea6691c27",
"_type":"item",
"_id":"513fceb375b8dbbc21000022",
"_score":11.854574,
"fields":{
"item_id":"513fceb375b8dbbc21000022",
"item_name":"Cheese, cheddar - 1 cup, diced",
"brand_name":"USDA",
"nf_calories":533.28,
"nf_total_fat":43.97,
"nf_cholesterol":130.68,
"nf_sodium":861.96,
"nf_serving_size_qty":1,
"nf_serving_size_unit":"serving"}},
{"_index":"f762ef22-e660-434f-9071-a10ea6691c27",
"_type":"item",
"_id":"513fceb375b8dbbc21000021",
"_score":11.800501,
"fields":{"item_id":"513fceb375b8dbbc21000021",
"item_name":"Cheese, cheddar - 1 cup, melted",
"brand_name":"USDA",
"nf_calories":985.76,
"nf_total_fat":81.28,
"nf_cholesterol":241.56,
"nf_sodium":1593.32,
"nf_serving_size_qty":1,
"nf_serving_size_unit":"serving"}}, ... etc.
我可以在第一部分訪問_Score值不過,我期待訪問的項目,如ITEM_NAME和nf_calories,但我不知道的語法。我相信我明白我需要做什麼,但我無法想象它在我腦海中。
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("hits");
List<NutritionModel> nutModelList = new ArrayList<>();
for(int i=0; i<parentArray.length(); i++) {
hitsObject = parentArray.getJSONObject(i);
NutritionModel nutModel = new NutritionModel();
nutModel.set_score(hitsObject.getDouble("_score"));
//Adding final object in the list
nutModelList.add(nutModel);
這是我目前在我的ViewNutrition.java文件中。
public class NutritionModel {
private Double _score;
public Array getFieldarray() {
return fieldarray;
}
public void setFieldarray(Array fieldarray) {
this.fieldarray = fieldarray;
}
private Array fieldarray;
private String item_name;
private Double nf_calories;
private Double nf_total_fat;
private Double nf_protein;
private Double nf_cholesterol;
private Double nf_sodium;
private Double nf_serving_size_qty;
public Double get_score() {
return _score;
}
public void set_score(Double _score) {
this._score = _score;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public Double getNf_calories() {
return nf_calories;
}
public void setNf_calories(Double nf_calories) {
this.nf_calories = nf_calories;
}
public Double getNf_total_fat() {
return nf_total_fat;
}
public void setNf_total_fat(Double nf_total_fat) {
this.nf_total_fat = nf_total_fat;
}
public Double getNf_protein() {
return nf_protein;
}
public void setNf_protein(Double nf_protein) {
this.nf_protein = nf_protein;
}
public Double getNf_cholesterol() {
return nf_cholesterol;
}
public void setNf_cholesterol(Double nf_cholesterol) {
this.nf_cholesterol = nf_cholesterol;
}
public Double getNf_sodium() {
return nf_sodium;
}
public void setNf_sodium(Double nf_sodium) {
this.nf_sodium = nf_sodium;
}
public Double getNf_serving_size_qty() {
return nf_serving_size_qty;
}
public void setNf_serving_size_qty(Double nf_serving_size_qty) {
this.nf_serving_size_qty = nf_serving_size_qty;
}
}
這是我的自定義類中使用JSON數據的當前內容。
我試過用for語句循環瀏覽JSONObject,搜索名爲「fields」的JSONObject,但它仍然只返回空結果。已經堅持了幾天,真的很掙扎,任何幫助或方向將不勝感激!謝謝。
這是我一直在試圖做的,但問題在於 JSONObject obj2 = parentArray.getJSONObject(「fields」); 由於getJSONObject需要一個int而不是「fields」,其中就好像它是一個使用getJSONArray(「fields」)的數組會更有意義,不幸的是不會感謝這個JSON格式。 – learning2program
它應該是JSONObject obj2 = obj1.getJSONObject(「fields」)而不是JSONObject obj2 = parentArray.getJSONObject(「fields」)。我已經編輯了答案。 –