繁体   English   中英

解析Json排球请求时出现问题

[英]Having issue while parsing Json volley request

这是我的请求代码,请帮助我正确解析它。 我想要每个特定问题的所有选项,但是我只能得到一个选项。任何人都可以解决它。 我已附上我的数据格式的SS。 在此处输入图片说明

JsonObjectRequest request = new JsonObjectRequest(
            "http://www.proprofs.com/quiz-school/mobileData/request.php?request=QuizStart&module=handShake&title=does-your-crush-like-you-girls-only_1",
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // ArrayList<Data> listData = new ArrayList<>();
                    if (response != null && response.length() > 0) {    

                        try {    
                            JSONArray array = response.getJSONArray("quiz");
                           // JSONObject jsonObject = array.getJSONObject(0);
                            for (int i = 0; i < array.length(); i++) {

                                JSONObject currentArray = array.getJSONObject(i);
                                DataQuestions movie = new DataQuestions();
                                movie.setQuestion(currentArray.getString("question"));
                                String question = currentArray.getString("question");
                                System.out.println("question----------" + question);
                                movie.setQuesImage(currentArray.getString("QuesImage"));
                                String image = currentArray.getString("QuesImage");
                                System.out.println("QuesImage----------" + image);    

                                 JSONArray keys= currentArray.getJSONArray("keys");
                                for(int j =0;j<keys.length();j++){
                                    JSONObject keyobject = keys.getJSONObject(j);

                                    movie.setOption(keyobject.getString("option"));}    

                                /*String key = null;

                                if(keys.getJSONObject("option")) {

                                    key = keys.getString("option");
                                }
                                movie.setOption(key);*/   

                               /* JSONArray jsonArray1 = (jsonObject.getJSONArray("keys"));
                                int numberOfItemsInResp = jsonArray1.length();
                                for (int j = 0; j < numberOfItemsInResp; j++) {
                                    JSONObject jsonObject2 = jsonArray1.getJSONObject(j);
                                    DataQuestions options = new DataQuestions();
                                    //   movie = new DataQuestions();

                                    options.setOption(jsonObject2.getString("option"));
                                    String optios = jsonObject2.getString("option");
                                    System.out.println("option----------" + options);
                                    // JSONArray jsonArray1 = (response.getJSONArray("keys"));*/

                                    movieList.add(movie);
                                }    
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }    
                    }
                    adapter.notifyDataSetChanged();    
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            //If an error occurs that means end of the list has reached
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            //  hidePDialog();
            Toast.makeText(MainActivity.this, "No Items Available", Toast.LENGTH_LONG).show();
        }
    });

    request.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(request);    
}

这是因为,您在第二个for循环中使用了一个对象。 您应该定义键对象并存储在主对象的arraylist中。 像这样

public class Quiz {
    String quizId;
    String QuesImage;
    String question;
    String questionId;
    String Type;
    int index;
    ArrayList<Keys> keysList;

}

public class Keys{
    String answerId;
    String option;
    String AnsImage;
}

并解析

JSONArray array = response.getJSONArray("quiz");

for (int i = 0; i < array.length(); i++){
    JSONObject jsonObj = array.getJSONObject(i);

    Quiz quiz = new Quiz();
    quiz.QuesImage = jsonObj.getString("QuesImage");
    quiz.question = jsonObj.getString("question");
    quiz.questionId = jsonObj.getString("questionId");
    quiz.Type = jsonObj.getString("Type");
    quiz.index = jsonObj.getInt("index");

    JSONArray keys = jsonObj.getJSONArray("keys");
    for (int j = 0; j < keys.length(); j++) {
        JSONObject keysObj = keys.getJSONObject(j);

        Keys keys = new Keys();
        keys.answerId = keysObj.getString("answerId");
        keys.option = keysObj.getString("option");
        keys.AnsImage = keysObj.getString("AnsImage");

        quiz.keysList.add(keys);
    }
}

暂无
暂无

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

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