英文:
How to Acces nested Json Array data in java
问题
我试图从Java中的JSON中提取数据。
有关如何访问这些已用蓝色下划线标出的JSON数据,您是否有任何想法?我猜这是嵌套的JSON数组?我知道JSON对象和getstring(key)的用法,但仅适用于红色下划线标出的数据。我想要那个蓝线下的数据。任何解决方案都将很好。对于我来说,任何帮助都将很有用,我已经在解决这个问题上工作了5天。
Java代码
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
response是一个包含JSON数据的字符串。
{
"calories": 115,
"totalWeight": 223.0,
"dietLabels": ["LOW_FAT"],
"healthLabels": ["VEGAN", "VEGETARIAN", "PEANUT_FREE", "TREE_NUT_FREE", "ALCOHOL_FREE", "SULPHITE_FREE"],
"cautions": ["SULFITES"],
"totalNutrients": {
"ENERC_KCAL": {"label": "Energy", "quantity": 115.96, "unit": "kcal"},
"FAT": {"label": "Fat", "quantity": 0.37910000000000005, "unit": "g"},
"FASAT": {"label": "Saturated", "quantity": 0.06244, "unit": "g"},
"FAMS": {"label": "Monounsaturated", "quantity": 0.01561, "unit": "g"},
"FAPU": {"label": "Polyunsaturated", "quantity": 0.11373, "unit": "g"},
"CHOCDF": {"label": "Carbs", "quantity": 30.796300000000002, "unit": "g"},
"FIBTG": {"label": "Fiber", "quantity": 5.351999999999999, "unit": "g"},
"SUGAR": {"label": "Sugars", "quantity": 23.169700000000002, "unit": "g"},
"PROCNT": {"label": "Protein", "quantity": 0.5798, "unit": "g"},
"CHOLE": {"label": "Cholesterol", "quantity": 0.0, "unit": "mg"}
}
}
英文:
I was trying to extract data from JSON in Java.
have any idea about how to access these JSON data which is underlined in blue it is nested JSON array I guess? I know about JSON object and getstring(key) but it is working only for these data which is underlined in red. I want that blueline data. Any solution would be great. Any help would be useful for me I am working on this issue for the last 5 days.
Java Code
JSONObject object = (JSONObject) new JSONTokener(response).nextValue();
response is a string which contain json data.
{
"calories":115,
"totalWeight":223.0,
"dietLabels":["LOW_FAT"],
"healthLabels":["VEGAN","VEGETARIAN","PEANUT_FREE","TREE_NUT_FREE","ALCOHOL_FREE","SULPHITE_FREE"],
"cautions":["SULFITES"],
"totalNutrients":{
"ENERC_KCAL": {"label":"Energy","quantity":115.96,"unit":"kcal"},
"FAT":{"label":"Fat","quantity":0.37910000000000005,"unit":"g"},
"FASAT":{"label":"Saturated","quantity":0.06244,"unit":"g"},
"FAMS":{"label":"Monounsaturated","quantity":0.01561,"unit":"g"},
"FAPU":{"label":"Polyunsaturated","quantity":0.11373,"unit":"g"},
"CHOCDF":{"label":"Carbs","quantity":30.796300000000002,"unit":"g"},
"FIBTG":{"label":"Fiber","quantity":5.351999999999999,"unit":"g"},
"SUGAR":{"label":"Sugars","quantity":23.169700000000002,"unit":"g"},
"PROCNT":{"label":"Protein","quantity":0.5798,"unit":"g"},
"CHOLE":{"label":"Cholesterol","quantity":0.0,"unit":"mg"}
}
}
答案1
得分: 0
你可以调用:
JSONArray dietLabels = jo.getJSONArray("dietLabels");
要访问 ENERC_KCAL 数据,你可以像这样操作:
JSONObject totalNutrients = jo.getJSONObject("totalNutrients");
JSONObject ENERC_KCAL = totalNutrients.getJSONObject("ENERC_KCAL");
在这里,jo 是主 JSON 对象(来自给定的 JSON 字符串),而 "dietLabels" 是数组的名称。
英文:
You can call
JSONArray dietLabels = jo.getJSONArray("dietLabels");
To access ENERC_KCAL Data you can do something like that :
JSONObject totalNutrients = jo.getJSONObject("totalNutrients");
JSONObject ENERC_KCAL = totalNutrients.getJSONObject("ENERC_KCAL");
on the JsonObject where jo is the main json object (from the given json string) and "dietLabels" is the name of the array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论