从改造解析JSON响应时获取错误并将其存储在领域中

时间:2017-08-27 05:32:53

标签: android realm retrofit

  

我正在从改造中解析这个json响应。没有扩展   RealmObject我可以成功解析,但当我将RealmObject扩展到   我的模型类得到错误。

     

我认为这里是“wrong_ans”:[]包含数组,所以我推出了MyObject   扩展RealmObject但eventhoght无法正常工作的类

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT但是为STRING

JSON RESPONE

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<li ng-repeat="item in data ">
    <span ng-bind-html="item.title | highlight:arrayFilter"></span>
  </li>
</body>

CRETE POJO CLASS

[
    {
        "testName": "super q test id",
        "wrong_ans": [],
        "correct_ans": [],
        "attempted_que": [
            "test-super-topic-1-2"
        ],
        "skipped_que": [
            "test-super-topic-1-2"
        ],
        "review_que": [],
        "totalQuestions": [
            "test-super-topic-1-1",
            "test-super-topic-1-2",
            "time-and-work-2-1",
            "time-and-work-2-2",
            "time-and-work-2-3",
            "time-and-work-2-4",
            "time-and-work-2-5"
        ],
        "test_id": "super-q-test-id"
    }
]

将对象类转换为MYOBJECT CLASS

public class FinishUserExam extends RealmObject implements Serializable{

    @SerializedName("testName")
    @Expose
    private String testName;
    @SerializedName("wrong_ans")
    @Expose
    private RealmList<MyObject> wrongAns = null;
    @SerializedName("correct_ans")
    @Expose
    private RealmList<MyObject> correctAns = null;
    @SerializedName("attempted_que")
    @Expose
    private RealmList<RealmString> attemptedQue = null;
    @SerializedName("skipped_que")
    @Expose
    private RealmList<RealmString> skippedQue = null;
    @SerializedName("review_que")
    @Expose
    private RealmList<MyObject> reviewQue = null;
    @SerializedName("totalQuestions")
    @Expose
    private RealmList<RealmString> totalQuestions = null;
    @SerializedName("test_id")
    @Expose
    private String testId;

    public String getTestName() {
        return testName;
    }

    public void setTestName(String testName) {
        this.testName = testName;
    }

    public RealmList<MyObject> getWrongAns() {
        return wrongAns;
    }

    public void setWrongAns(RealmList<MyObject> wrongAns) {
        this.wrongAns = wrongAns;
    }

    public RealmList<MyObject> getCorrectAns() {
        return correctAns;
    }

    public void setCorrectAns(RealmList<MyObject> correctAns) {
        this.correctAns = correctAns;
    }

    public RealmList<RealmString> getAttemptedQue() {
        return attemptedQue;
    }

    public void setAttemptedQue(RealmList<RealmString> attemptedQue) {
        this.attemptedQue = attemptedQue;
    }

    public RealmList<RealmString> getSkippedQue() {
        return skippedQue;
    }

    public void setSkippedQue(RealmList<RealmString> skippedQue) {
        this.skippedQue = skippedQue;
    }

    public RealmList<MyObject> getReviewQue() {
        return reviewQue;
    }

    public void setReviewQue(RealmList<MyObject> reviewQue) {
        this.reviewQue = reviewQue;
    }

    public RealmList<RealmString> getTotalQuestions() {
        return totalQuestions;
    }

    public void setTotalQuestions(RealmList<RealmString> totalQuestions) {
        this.totalQuestions = totalQuestions;
    }

    public String getTestId() {
        return testId;
    }

    public void setTestId(String testId) {
        this.testId = testId;
    }


}

1 个答案:

答案 0 :(得分:0)

你应该删除&#39; [&#39;和&#39;]&#39;在你的json的开头和结尾,所以你的json应该是这样的

{
        "testName": "super q test id",
        "wrong_ans": [],
        "correct_ans": [],
        "attempted_que": [
            "test-super-topic-1-2"
        ],
        "skipped_que": [
            "test-super-topic-1-2"
        ],
        "review_que": [],
        "totalQuestions": [
            "test-super-topic-1-1",
            "test-super-topic-1-2",
            "time-and-work-2-1",
            "time-and-work-2-2",
            "time-and-work-2-3",
            "time-and-work-2-4",
            "time-and-work-2-5"
        ],
        "test_id": "super-q-test-id"
    }

不是这个

[
    {
        "testName": "super q test id",
        "wrong_ans": [],
        "correct_ans": [],
        "attempted_que": [
            "test-super-topic-1-2"
        ],
        "skipped_que": [
            "test-super-topic-1-2"
        ],
        "review_que": [],
        "totalQuestions": [
            "test-super-topic-1-1",
            "test-super-topic-1-2",
            "time-and-work-2-1",
            "time-and-work-2-2",
            "time-and-work-2-3",
            "time-and-work-2-4",
            "time-and-work-2-5"
        ],
        "test_id": "super-q-test-id"
    }
]