尽管JSON正确,但仍出现“预期的BEGIN_ARRAY但为BEGIN_OBJECT”错误

时间:2019-10-11 09:32:10

标签: java android json retrofit retrofit2

我已经查看了所有其他与此有关的stackoverflow主题,并且它们在被调用的JSON中几乎总是存在相同的缺陷,即JSON仅包含一个对象,而不包含数组。我已确定要解决此问题,在下面您可以看到在Postman中执行查询后返回的JSON。

我已经用尽了所有的解决方案,因为每个在线解决方案都涉及将JSON转换为数组,就像我已经做过的那样。我在网上找到了有关如何使用改型(Call<List<Object>>来调用列表的教程,该教程使用几乎相同的代码进行挖掘,并且JSON的格式相同,所以我认为我的JSON是正确的

首先,返回JSON:

[
  {
    "is_active": 1,
    "match_date": "Wed, 09 Oct 2019 13:53:27 GMT",
    "match_id": 2,
    "match_winner_user_id": null,
    "rnd1_cat1": "test category1",
    "rnd1_cat1_q1": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 1,
      "question_text": "test question text1",
      "user1_answer": 1,
      "user2_answer": 2
    },
    "rnd1_cat1_q1_user1_answer": 1,
    "rnd1_cat1_q1_user2_answer": 2,
    "rnd1_cat1_q2": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 2,
      "question_text": "test question text2",
      "user1_answer": 2,
      "user2_answer": 2
    },
    "rnd1_cat1_q2_user1_answer": 2,
    "rnd1_cat1_q2_user2_answer": 2,
    "rnd1_cat1_q3": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 3,
      "question_text": "test question text3",
      "user1_answer": 5,
      "user2_answer": 4
    },
    "rnd1_cat1_q3_user1_answer": 5,
    "rnd1_cat1_q3_user2_answer": 4,
    "rnd1_cat2": "test category2",
    "rnd1_cat2_q1": {
      "category_id": 2,
      "correct_answer": {
        "answer_id": 6,
        "answer_text": "test answer6",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 4,
      "question_text": "test question text4",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q1_user1_answer": null,
    "rnd1_cat2_q1_user2_answer": null,
    "rnd1_cat2_q2": {
      "category_id": 3,
      "correct_answer": {
        "answer_id": 6,
        "answer_text": "test answer6",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 5,
      "question_text": "test question text5",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q2_user1_answer": null,
    "rnd1_cat2_q2_user2_answer": null,
    "rnd1_cat2_q3": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 5,
        "answer_text": "test answer5",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 6,
      "question_text": "test question text6",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q3_user1_answer": null,
    "rnd1_cat2_q3_user2_answer": null,
    "rnd2_cat1": null,
    "rnd2_cat1_q1": null,
    "rnd2_cat1_q1_user1_answer": null,
    "rnd2_cat1_q1_user2_answer": null,
    "rnd2_cat1_q2": null,
    "rnd2_cat1_q2_user1_answer": null,
    "rnd2_cat1_q2_user2_answer": null,
    "rnd2_cat1_q3": null,
    "rnd2_cat1_q3_user1_answer": null,
    "rnd2_cat1_q3_user2_answer": null,
    "rnd2_cat2": null,
    "rnd2_cat2_q1": null,
    "rnd2_cat2_q1_user1_answer": null,
    "rnd2_cat2_q1_user2_answer": null,
    "rnd2_cat2_q2": null,
    "rnd2_cat2_q2_user1_answer": null,
    "rnd2_cat2_q2_user2_answer": null,
    "rnd2_cat2_q3": null,
    "rnd2_cat2_q3_user1_answer": null,
    "rnd2_cat2_q3_user2_answer": null,
    "user1_id": 36,
    "user2_id": 21
  },
  {
    "is_active": 1,
    "match_date": "Wed, 09 Oct 2019 13:53:27 GMT",
    "match_id": 2,
    "match_winner_user_id": null,
    "rnd1_cat1": "test category1",
    "rnd1_cat1_q1": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 1,
      "question_text": "test question text1",
      "user1_answer": 1,
      "user2_answer": 2
    },
    "rnd1_cat1_q1_user1_answer": 1,
    "rnd1_cat1_q1_user2_answer": 2,
    "rnd1_cat1_q2": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 2,
      "question_text": "test question text2",
      "user1_answer": 2,
      "user2_answer": 2
    },
    "rnd1_cat1_q2_user1_answer": 2,
    "rnd1_cat1_q2_user2_answer": 2,
    "rnd1_cat1_q3": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 3,
        "answer_text": "test answer3",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 3,
      "question_text": "test question text3",
      "user1_answer": 5,
      "user2_answer": 4
    },
    "rnd1_cat1_q3_user1_answer": 5,
    "rnd1_cat1_q3_user2_answer": 4,
    "rnd1_cat2": "test category2",
    "rnd1_cat2_q1": {
      "category_id": 2,
      "correct_answer": {
        "answer_id": 6,
        "answer_text": "test answer6",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 4,
      "question_text": "test question text4",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q1_user1_answer": null,
    "rnd1_cat2_q1_user2_answer": null,
    "rnd1_cat2_q2": {
      "category_id": 3,
      "correct_answer": {
        "answer_id": 6,
        "answer_text": "test answer6",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 5,
      "question_text": "test question text5",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q2_user1_answer": null,
    "rnd1_cat2_q2_user2_answer": null,
    "rnd1_cat2_q3": {
      "category_id": 1,
      "correct_answer": {
        "answer_id": 2,
        "answer_text": "test answer2",
        "is_active": 1
      },
      "incorrect_answer1": {
        "answer_id": 5,
        "answer_text": "test answer5",
        "is_active": 1
      },
      "incorrect_answer2": {
        "answer_id": 4,
        "answer_text": "test answer4",
        "is_active": 1
      },
      "incorrect_answer3": {
        "answer_id": 1,
        "answer_text": "test answer1",
        "is_active": 1
      },
      "is_active": 1,
      "question_id": 6,
      "question_text": "test question text6",
      "user1_answer": null,
      "user2_answer": null
    },
    "rnd1_cat2_q3_user1_answer": null,
    "rnd1_cat2_q3_user2_answer": null,
    "rnd2_cat1": null,
    "rnd2_cat1_q1": null,
    "rnd2_cat1_q1_user1_answer": null,
    "rnd2_cat1_q1_user2_answer": null,
    "rnd2_cat1_q2": null,
    "rnd2_cat1_q2_user1_answer": null,
    "rnd2_cat1_q2_user2_answer": null,
    "rnd2_cat1_q3": null,
    "rnd2_cat1_q3_user1_answer": null,
    "rnd2_cat1_q3_user2_answer": null,
    "rnd2_cat2": null,
    "rnd2_cat2_q1": null,
    "rnd2_cat2_q1_user1_answer": null,
    "rnd2_cat2_q1_user2_answer": null,
    "rnd2_cat2_q2": null,
    "rnd2_cat2_q2_user1_answer": null,
    "rnd2_cat2_q2_user2_answer": null,
    "rnd2_cat2_q3": null,
    "rnd2_cat2_q3_user1_answer": null,
    "rnd2_cat2_q3_user2_answer": null,
    "user1_id": 36,
    "user2_id": 21
  }
]

和接口类:

    @POST("/api/v1/match/getMatches")
    Call<List<Match>> getMatches(@Header("Authorization") String authorization);

Match类,仅用于注释对象和自动生成的吸气剂:

import com.google.gson.annotations.SerializedName;

public class Match {

    @SerializedName("match_id")
    private int matchId;

    @SerializedName("user1_id")
    private int user1Id;
    @SerializedName("user2_id")
    private int user2Id;

    // Categories
    @SerializedName("rnd1_cat1")
    private String rnd1_cat1;
    @SerializedName("rnd1_cat2")
    private String rnd1_cat2;
    @SerializedName("rnd2_cat1")
    private String rnd2_cat1;
    @SerializedName("rnd2_cat2")
    private String rnd2_cat2;

    //Questions, which contain answers objects within them
    @SerializedName("rnd1_cat1_q1")
    private Question rnd1_cat1_q1;
    @SerializedName("rnd1_cat1_q2")
    private Question rnd1_cat1_q2;
    @SerializedName("rnd1_cat1_q3")
    private Question rnd1_cat1_q3;
    //Cat2
    @SerializedName("rnd1_cat2_q1")
    private Question rnd1_cat2_q1;
    @SerializedName("rnd1_cat2_q2")
    private Question rnd1_cat2_q2;
    @SerializedName("rnd1_cat2_q3")
    private Question rnd1_cat2_q3;
    //Round 2
    @SerializedName("rnd2_cat1_q1")
    private Question rnd2_cat1_q1;
    @SerializedName("rnd2_cat1_q2")
    private Question rnd2_cat1_q2;
    @SerializedName("rnd2_cat1_q3")
    private Question rnd2_cat1_q3;
    //Cat2
    @SerializedName("rnd2_cat2_q1")
    private Question rnd2_cat2_q1;
    @SerializedName("rnd2_cat2_q2")
    private Question rnd2_cat2_q2;
    @SerializedName("rnd2_cat2_q3")
    private Question rnd2_cat2_q3;

    public int getMatchId() {
        return matchId;
    }

    public int getUser1Id() {
        return user1Id;
    }

    public int getUser2Id() {
        return user2Id;
    }

    public String getRnd1_cat1() {
        return rnd1_cat1;
    }

    public String getRnd1_cat2() {
        return rnd1_cat2;
    }

    public String getRnd2_cat1() {
        return rnd2_cat1;
    }

    public String getRnd2_cat2() {
        return rnd2_cat2;
    }

    public Question getRnd1_cat1_q1() {
        return rnd1_cat1_q1;
    }

    public Question getRnd1_cat1_q2() {
        return rnd1_cat1_q2;
    }

    public Question getRnd1_cat1_q3() {
        return rnd1_cat1_q3;
    }

    public Question getRnd1_cat2_q1() {
        return rnd1_cat2_q1;
    }

    public Question getRnd1_cat2_q2() {
        return rnd1_cat2_q2;
    }

    public Question getRnd1_cat2_q3() {
        return rnd1_cat2_q3;
    }

    public Question getRnd2_cat1_q1() {
        return rnd2_cat1_q1;
    }

    public Question getRnd2_cat1_q2() {
        return rnd2_cat1_q2;
    }

    public Question getRnd2_cat1_q3() {
        return rnd2_cat1_q3;
    }

    public Question getRnd2_cat2_q1() {
        return rnd2_cat2_q1;
    }

    public Question getRnd2_cat2_q2() {
        return rnd2_cat2_q2;
    }

    public Question getRnd2_cat2_q3() {
        return rnd2_cat2_q3;
    }
}

问题类:

import com.google.gson.annotations.SerializedName;

public class Question {

    @SerializedName("question_id")
    private int questionId;

    @SerializedName("category_name")
    private String category;

    @SerializedName("question_text")
    private String questionText;

    @SerializedName("correct_answer")
    private Answer correctAnswer;

    @SerializedName("incorrect_answer1")
    private Answer incorrectAnswer1;

    @SerializedName("incorrect_answer2")
    private Answer incorrectAnswer2;

    @SerializedName("incorrect_answer3")
    private Answer incorrectAnswer3;


    public int getQuestionId() {
        return questionId;
    }

    public String getCategory() {
        return category;
    }

    public String getQuestionText() {
        return questionText;
    }

    public Answer getCorrectAnswer() {
        return correctAnswer;
    }

    public Answer getIncorrectAnswer1() {
        return incorrectAnswer1;
    }

    public Answer getIncorrectAnswer2() {
        return incorrectAnswer2;
    }

    public Answer getIncorrectAnswer3() {
        return incorrectAnswer3;
    }

}

和Answer类:

import com.google.gson.annotations.SerializedName;

public class Answer {

    @SerializedName("answer_id")
    private int answerId;
    @SerializedName("answer_text")
    private String answerText;

    public int getAnswerId() {
        return answerId;
    }

    public String getAnswerText() {
        return answerText;
    }

}

最后是电话:

Call<List<Match>> call = backendApi.getMatches(auth_token);
        call.enqueue(new Callback<List<Match>>() {
            @Override
            public void onResponse(Call<List<Match>> call, Response<List<Match>> response) {
                if(!response.isSuccessful()){
                    Log.e("login activity", "2");
                    return;
                }
                List<Match> matchList = response.body();
                Log.i("match list", matchList.size()+"");



            }

            @Override
            public void onFailure(Call<List<Match>> call, Throwable t) {
                Log.e("retrofit", t.getMessage());
            }
        });

由于某种原因,我得到了一个错误:预期是BEGIN_ARRAY,但在第1行第2列路径处是BEGIN_OBJECT,尽管据我所知一切正常。

非常感谢您的帮助!

编辑:我似乎注意到Postman和Retrofit认为Header中的Content-Lengths不同。邮递员会看到一个真正的值,对于json中的一次匹配,它的值为5000+,并且Retrofit无论如何都返回64的Content-Length。奇怪的。

0 个答案:

没有答案