Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何解析这个JSON对象,以获得我可以使用的配方对象列表?_Java_Json_Android Studio_Parsing - Fatal编程技术网

Java 我如何解析这个JSON对象,以获得我可以使用的配方对象列表?

Java 我如何解析这个JSON对象,以获得我可以使用的配方对象列表?,java,json,android-studio,parsing,Java,Json,Android Studio,Parsing,因此,我对Java非常陌生,需要帮助解析JSON响应。我已经有了这样做的方法,但需要帮助找出我到底做错了什么 我正在使用API从站点检索食谱。我正在尝试获取一个食谱列表(在本例中为“点击次数”),然后向用户展示 显然,我需要配方名称(本例中为“标签”)和其他信息。有人能帮我吗 下面是我得到的api响应: { "q": "chicken", "from": 0, "to": 10, "more": true, "count": 168106, "hits": [

因此,我对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;
        }
    }
public void onResponse(调用、响应)抛出IOException{
if(response.issusccessful()){
最后一个字符串myResponse=response.body().String();
getActivity().runOnUiThread(新的Runnable()){
私有对象日期;
@凌驾
公开募捐{
字符串jsonData=myResponse;
query_result.setText(myResponse);
日志d(“响应5”,myResponse);
//JSONObject jsonData1=新的JSONObject(myResponse);
JSONObject Jobject=null;
试一试{
Jobject=新的JSONObject(jsonData);
Log.d(“jobject”,jobject.toString());
}捕获(JSONException e){
e、 printStackTrace();
}
JSONArray-Jarray=null;
试一试{
Jarray=Jobject.getJSONArray(“hits”);
}捕获(JSONException e){
e、 printStackTrace();
}
for(int i=0;i
注意{}{}只是不同的配方……它不适合整个回答


目前,当我试图在日志中打印变量时,我收到一条消息,说变量为空(我用来存储不同配方的变量/“点击数”。

您的响应似乎不完整,它不是正确的JSON格式,请提供正确的格式,我将尽力帮助您

     String s = "{\n" +
            "    \"a1\":\"1\",\n" +
            "    \"b2\":[\n" +
            "        {\n" +
            "            \"b21\":\"vb21\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"b22\":\"vb22\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";
    JSONObject object = JSON.parseObject(s);
    String vb21 = object.getJSONArray("b2").getJSONObject(0).getString("b21");
像这样,neep需要导入com.alibaba.fastjson
但是我不知道你是否可以使用fastjson

这是我从API中检索到的原始响应。还有什么我可以做的吗?缺少了什么?不管怎样,我找到了问题所在。我将编辑我的帖子以包含整个响应。JSON格式的字符串必须有完整的“{}”[]'表示它是一个数组或对象,从'{',或'['开始,现在应该修复任何想法?我最近修复了响应。请注意,空的{}只是更多的配方。我无法适应整个响应,因为它有太多字符{“a1”:“1”,“b2”:[{“b21”:“vb21”},{“b22”:“vb22”}]}