2016-12-15 109 views
1

我有我想用於我的大學項目的移動應用程序的這個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,但它仍然只返回空結果。已經堅持了幾天,真的很掙扎,任何幫助或方向將不勝感激!謝謝。

回答

0

爲了訪問像item_name和nf_calories這樣的項目,需要去訪問內部數組。那麼,如何做 -

JSONObject parentObject = new JSONObject(finalJson); 
JSONArray parentArray = parentObject.getJSONArray("hits"); 

假設你需要訪問的「命中」的元素(ITEM_NAME等)的索引i然後,你需要去命名爲「田」內的JSONObject,然後你需要訪問名爲鍵「ITEM_NAME」 該做 -

for(int i=0;i<parentArray.length();i++) { 
    JSONObject obj1=parentArray.getJSONObject(i); 
    JSONObject obj2= obj1.getJSONObject("fields"); 
    String item_name=obj2.getString("item_name"); 
} 

不管怎麼說,即使你的解釋感到困惑,那麼更好的可視化JSON訪問這個網站 - http://jsonviewer.stack.hu/

+0

這是我一直在試圖做的,但問題在於 JSONObject obj2 = parentArray.getJSONObject(「fields」); 由於getJSONObject需要一個int而不是「fields」,其中就好像它是一個使用getJSONArray(「fields」)的數組會更有意義,不幸的是不會感謝這個JSON格式。 – learning2program

+0

它應該是JSONObject obj2 = obj1.getJSONObject(「fields」)而不是JSONObject obj2 = parentArray.getJSONObject(「fields」)。我已經編輯了答案。 –

0

Field對象不是一個數組,它是一個對象。我建議你創建一個FieldModel類並讓NutritionModel類包含一個FieldModel類的實例。

// In parser class 
nutModel.setFields(hitsObject.getJSONObject("fields")); 

// In NutritionModel class 
public void setFields(JSONObject obj) { 
    this.fields.item_name = obj.getString("item_name"); 
    // etc 
} 
+0

我覺得像這樣的解決方案將是乾淨的執行,但我比較新的使用課程,只是開始編程自己。當你說創建一個FieldModel類時,你的意思是在NutritionModel內部創建一個類並使用其中的變量來獲取和設置「字段」部分中的每個需要的項目? 如果是這樣,那麼將在哪裏 'this.fields.item_name = obj.getString(「item_name」);' 從哪裏來? – learning2program