我有这个 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,但它仍然只返回 null 结果。现在被困了几天,真的很挣扎,任何帮助或指导将不胜感激!谢谢。
最佳答案
为了访问 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/
关于java - 如何访问 JSONObject、JSONObject 和 JSONArray 中的内容? Java/安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41173070/