gpt4 book ai didi

java - 如何解析此 JSON 对象以获取可以使用的配方对象列表?

转载 作者:行者123 更新时间:2023-12-02 08:45:27 33 4
gpt4 key购买 nike

所以我对 Java 非常陌生,需要帮助解析 JSON 响应。我已经有了我用来这样做的方法,但需要帮助找出我到底做错了什么。

我正在使用 API 从网站检索食谱。我正在尝试获取食谱列表(我假设在这种情况下为“点击”?),然后我可以将其显示给用户。

显然我需要食谱名称(在本例中为“标签”)和其他信息。谁能帮我吗?

这是我收到的响应 api 响应:

{
"q": "chicken",
"from": 0,
"to": 10,
"more": true,
"count": 168106,
"hits": [
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_b79327d05b8e5b838ad6cfd9576b30b6",
"label": "Chicken Vesuvio",
"image": "https://www.edamam.com/web-img/e42/e42f9119813e890af34c259785ae1cfb.jpg",
"source": "Serious Eats",
"url": "http://www.seriouseats.com/recipes/2011/12/chicken-vesuvio-recipe.html",
"shareAs": "http://www.edamam.com/recipe/chicken-vesuvio-b79327d05b8e5b838ad6cfd9576b30b6/chicken",
"yield": 4,
"dietLabels": [
"Low-Carb"
],
"healthLabels": [],
"cautions": [],
"ingredientLines": [
"1/2 cup olive oil",
"5 cloves garlic, peeled",
"2 large russet potatoes, peeled and cut into chunks",
"1 3-4 pound chicken, cut into 8 pieces (or 3 pound chicken legs)",
"3/4 cup white wine",
"3/4 cup chicken stock",
"3 tablespoons chopped parsley",
"1 tablespoon dried oregano",
"Salt and pepper",
"1 cup frozen peas, thawed"
],
"ingredients": [
{
"text": "1/2 cup olive oil",
"weight": 108
},
{
"text": "5 cloves garlic, peeled",
"weight": 15
},
{
"text": "2 large russet potatoes, peeled and cut into chunks",
"weight": 532.5
},
{
"text": "1 3-4 pound chicken, cut into 8 pieces (or 3 pound chicken legs)",
"weight": 1587.5732
},
{
"text": "3/4 cup white wine",
"weight": 169.5
},
{
"text": "3/4 cup chicken stock",
"weight": 180
},
{
"text": "3 tablespoons chopped parsley",
"weight": 11.4
},
{
"text": "1 tablespoon dried oregano",
"weight": 6
},
{
"text": "Salt and pepper",
"weight": 16.46384
},
{
"text": "Salt and pepper",
"weight": 8.23192
},
{
"text": "1 cup frozen peas, thawed",
"weight": 134
}
],
"calories": 4055.7632,
"totalWeight": 2765.59,
"totalTime": 60,
"totalNutrients": {},
"totalDaily": {},
"digest": []
},
"bookmarked": false,
"bought": false
},
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_8275bb28647abcedef0baaf2dcf34f8b",
"label": "Chicken Paprikash",
"image": "https://www.edamam.com/web-img/e12/e12b8c5581226d7639168f41d126f2ff.jpg",
"source": "No Recipes",
"url": "http://norecipes.com/recipe/chicken-paprikash/",
"shareAs": "http://www.edamam.com/recipe/chicken-paprikash-8275bb28647abcedef0baaf2dcf34f8b/chicken",
"yield": 4,
"dietLabels": [],
"healthLabels": [],
"cautions": [],
"ingredientLines": [],
"ingredients": [],
"calories": 3033.2012,
"totalWeight": 1824.6125,
"totalTime": 0,
"totalNutrients": {},
"totalDaily": {},
"digest": []
},
"bookmarked": false,
"bought": false
},
{
"recipe": {
"uri": "http://www.edamam.com/ontologies/edamam.owl#recipe_584ac5e486c088b3c8409c252d7f290c",
"label": "Chicken Gravy",
"image": "https://www.edamam.com/web-img/fd1/fd1afed1849c44f5185720394e363b4e.jpg",
"source": "Martha Stewart",
"url": "http://www.marthastewart.com/332664/chicken-gravy",
"shareAs": "http://www.edamam.com/recipe/chicken-gravy-584ac5e486c088b3c8409c252d7f290c/chicken",
"yield": 6,
"dietLabels": [],
"healthLabels": [],
"cautions": [],
"ingredientLines": [],
"ingredients": [],
"calories": 1092.3606,
"totalWeight": 1590.8628,
"totalTime": 270,
"totalNutrients": {},
"totalDaily": {},
"digest": []
},
"bookmarked": false,
"bought": false
},
{},
{},
{},
{},
{},
{},
{}
]
}

这是我目前用来解析 JSON 对象的方法:

public void onResponse(Call call, Response response) throws IOException {

if (response.isSuccessful()) {
final String myResponse = response.body().string();
getActivity().runOnUiThread(new Runnable() {
private Object Date;

@Override
public void run() {
String jsonData = myResponse;
query_result.setText(myResponse);
Log.d("response5", myResponse);
//JSONObject jsonData1 = new JSONObject(myResponse);
JSONObject Jobject = null;
try {
Jobject = new JSONObject(jsonData);
Log.d("jobject", Jobject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray Jarray = null;
try {
Jarray = Jobject.getJSONArray("hits");
} catch (JSONException e) {
e.printStackTrace();
}

for (int i = 0; i < Jarray.length(); i++) {
JSONObject object;
try {
object = Jarray.getJSONObject(i);
try {
JSONArray Recipe = object.getJSONArray("recipe");
}catch (JSONException e){
e.printStackTrace();
}
for (int j = 0; j < Jarray.length(); j++){
JSONObject object2 = null;
try {
object2 = Jarray.getJSONObject(j);
String Name = object2.getString("label");
String Photo = object2.getString("image");
Log.d("rec_query", " " + Photo);
}catch (JSONException e){
e.printStackTrace();
Log.d("response6:", e.toString());
}
}

} catch (JSONException e) {
e.printStackTrace();
}
}

}
});
}
}
});

return null;
}
}

注意 {} {} 只是不同的配方...它不适合整个响应

目前,当我尝试在日志中打印变量时,我收到一条消息,指出该变量为空(我用来存储不同食谱/“命中”的变量。

最佳答案

您的回复似乎不完整,它的 JSON 格式不正确,请提供正确的格式,我会尽力帮助您

关于java - 如何解析此 JSON 对象以获取可以使用的配方对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61114401/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com