【问题标题】:How to access what appears to be JSONObject, in JSONObject, in a JSONArray? Java/Android如何在 JSONObject 和 JSONArray 中访问看似 JSONObject 的内容? Java/安卓
【发布时间】:2017-05-01 13:57:45
【问题描述】:

我有这个 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,搜索名为“fields”的 JSONObject,但它仍然只返回空结果。现在被困了几天并且真的很挣扎,任何帮助或指导将不胜感激!谢谢。

【问题讨论】:

    标签: java android arrays json parsing


    【解决方案1】:

    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
    }
    

    【讨论】:

    • 我觉得这个解决方案执行起来很干净,但是我对使用类还比较陌生,我才刚刚开始自己​​编程。当您说创建 FieldModel 类时,您的意思是在 NutritionModel 内部创建一个类并使用其中的变量来获取和设置“字段”部分中的每个所需项目吗?如果是这样,那么'this.fields.item_name = obj.getString("item_name");'从哪里来?
    【解决方案2】:

    为了访问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/

    【讨论】:

    • 这是我一直在尝试做的,但问题在于 JSONObject obj2= parentArray.getJSONObject("fields");因为 getJSONObject 需要一个 int 而不是“fields”,所以如果它是一个使用 getJSONArray("fields") 的数组会更有意义,不幸的是,这要归功于这种 JSON 格式。
    • 它应该是 JSONObject obj2= obj1.getJSONObject("fields") 而不是 JSONObject obj2= parentArray.getJSONObject("fields")。我已经在答案中进行了编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多