简体   繁体   English

如何访问JSONArray中的JSONObject中的JSONObject? Java /安卓

[英]How to access what appears to be JSONObject, in JSONObject, in a JSONArray? Java/Android

I have this JSON file which I would like to use for my mobile application for my university project, however I'm having issues accessing the "fields" section of it and I'm not sure the best way to access it. 我有这个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.

I can access the _Score value in the first section however, I am looking to access items like item_name, and nf_calories, however I'm not sure on the syntax. 我可以在第一部分中访问_Score值,但是,我希望访问诸如item_name和nf_calories之类的项,但是我不确定语法。 I believe I understand what I need to do, but I can't visualise it in my head. 我相信我了解我需要做什么,但是我无法在脑海中将其形象化。

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);

This is what I currently have in my ViewNutrition.java file. 这是我当前在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;
    }
}

This is what's currently in my custom class for using the JSON data. 这是我的自定义类中当前使用JSON数据的内容。

I've tried cycling through a JSONObject with a for statement, searching for a JSONObject called "fields" however it still just returns null results. 我尝试过使用for语句遍历JSONObject,搜索名为“ fields”的JSONObject,但是它仍然只返回空结果。 Been stuck for a couple of days now and really struggling, any help or direction would be appreciated! 现在被困了几天,真的很挣扎,任何帮助或指导将不胜感激! Thanks. 谢谢。

The Field object isn't an Array it's an object. Field对象不是数组,而是对象。 I suggest you create a FieldModel class and have the NutritionModel class contain an instance of the FieldModel class. 我建议您创建一个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
}

In order to access items like item_name, and nf_calories use need to go to access the inner array. 为了访问诸如item_name和nf_calories之类的项,需要访问内部数组。 So, how to do- 因此,该怎么做-

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

Suppose you need to access the elements(item_name etc.) of "hits" at index i then, you need to go to the inner JSONObject named "fields" and then you need to access the key named "item_name" for that do- 假设您需要访问索引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");
}

Anyways even if you get confused in the explanation then for better JSON visualization access this website - http://jsonviewer.stack.hu/ 无论如何,即使您对解释感到困惑,也可以通过以下网站获得更好的JSON可视化效果: http://jsonviewer.stack.hu/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM