如何在JSONArray中访问JSONObject中看起来像JSONObject的东西?的Java / Android的

时间:2016-12-15 20:46:49

标签: java android arrays json parsing

我有这个JSON文件,我希望将其用于我的大学项目的移动应用程序,但是我在访问"字段时遇到问题"它的部分和我不确定访问它的最佳方式。

{"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,搜索名为&#34;字段&#34;的JSONObject。但它仍然只返回null结果。已经卡住了几天而且真的很挣扎,任何帮助或方向都会受到赞赏!感谢。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

为了访问item_name和nf_calories之类的项目,需要去访问内部数组。那么,怎么做 -

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

假设您需要访问索引i处“hits”的元素(item_name等),那么您需要转到名为“fields”的内部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/