使用Jackson和Parcelable解析嵌套自定义对象数组

3
我将尝试使用Jackson将一个JSON解析成Java Bean。
服务器端的JSON代码片段如下:
{
"count": 17,
"next": null,
"previous": null,
"results": [
  {
        "resource_uri": "http://www.randomsite.com/api/1/personalize/qna/17/",
        "answer_set": [],
        "answers_count": 0,
        "tags": [
            "Others"
        ],
        "title": "How many boys hostels are there is ACMS , which one is the best one",
        "desc": "How many boys hostel are there in ACMS and which one is good among them, How can i get them, Do i have to apply early or can apply any any time in session. ",
        "status": 1,
        "is_spam": false,
        "upvotes": 0,
        "downvotes": 0,
        "view_count": 4,
        "uri_slug": "how-many-boys-hostels-are-there-is-acms-which-one-is-the-best-one",
        "added_on": "2015-06-05T16:43:26",
        "user": "http://www.randomsite.com/api/1/users/1/",
        "degree": null,
        "stream": "http://www.randomsite.com/api/1/streams/16/",
        "institute": null,
        "course": null,
        "city": null,
        "state": null
    },
    {
        "resource_uri": "http://www.randomsite.com/api/1/personalize/qna/18/",
        "answer_set": [
            {
                "id": 5,
                "resource_uri": "http://www.randomsite.com/api/1/personalize/answers/5/",
                "question": "http://www.randomsite.com/api/1/personalize/qna/18/",
                "answer_text": "You will get purified water, volley ball court, cricket ground, gymnasium, tt, carrom , chess, parking, mess, canteen",
                "status": 1,
                "is_spam": false,
                "upvotes": 0,
                "downvotes": 0,
                "best_answer": false,
                "added_on": "2015-06-05T17:00:26",
                "user": "http://www.randomsite.com/api/1/users/1/"
            },
            {
                "id": 6,
                "resource_uri": "http://www.randomsite.com/api/1/personalize/answers/6/",
                "question": "http://www.randomsite.com/api/1/personalize/qna/18/",
                "answer_text": "You can call your friends over there but girls are not allowed.",
                "status": 1,
                "is_spam": false,
                "upvotes": 0,
                "downvotes": 0,
                "best_answer": false,
                "added_on": "2015-06-05T17:00:26",
                "user": "http://www.randomsite.com/api/1/users/1/"
            }
        ],
        "answers_count": 2,
        "tags": [],
        "title": "Girls are allowed in boys hostels of ACMS",
        "desc": "If I manage to get Boys hostel in ACMS what are the faciclities that i will get, can i call my friends there which include girls.",
        "status": 1,
        "is_spam": false,
        "upvotes": 0,
        "downvotes": 2,
        "view_count": 106,
        "uri_slug": "girls-are-allowed-in-boys-hostels-of-acms",
        "added_on": "2015-06-05T16:43:53",
        "user": "http://www.randomsite.com/api/1/users/1/",
        "degree": null,
        "stream": null,
        "institute": null,
        "course": null,
        "city": null,
        "state": null
    }
}

这是问题实体类:

public class QnAQuestions implements Parcelable {
public static final Creator<QnAQuestions> CREATOR = new Creator<QnAQuestions>() {
    @Override
    public QnAQuestions createFromParcel(Parcel source) {
        return new QnAQuestions(source);
    }

    @Override
    public QnAQuestions[] newArray(int size) {
        return new QnAQuestions[size];
    }
};

public String resource_uri;
public ArrayList<QnAAnswers> answer_set;
public int answers_count;
public ArrayList<String> tags;
public String title;
public String desc;
public int status;
public boolean is_spam;
public int upvotes;
public int downvotes;
public int view_count;
public String uri_slug;
public String added_on;
public String user;
public String degree;
public String stream;
public String institute;
public String course;
public String city;
public String state;

public QnAQuestions() {
    //answer_set = new ArrayList<QnAAnswers>();
}

public QnAQuestions(Parcel source) {
    resource_uri = source.readString();
    answers_count = source.readInt();
    title = source.readString();
    desc = source.readString();
    status = source.readInt();
    tags = source.createStringArrayList();
    is_spam = source.readString() == "true";
    upvotes = source.readInt();
    downvotes = source.readInt();
    view_count = source.readInt();
    uri_slug = source.readString();
    added_on = source.readString();
    user = source.readString();
    degree = source.readString();
    stream = source.readString();
    institute = source.readString();
    course = source.readString();
    city = source.readString();
    state = source.readString();

    //answer_set = new ArrayList<QnAAnswers>();
    answer_set = source.createTypedArrayList(QnAAnswers.CREATOR);
    source.readTypedList(answer_set, QnAAnswers.CREATOR);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(resource_uri);
    dest.writeInt(answers_count);
    dest.writeString(title);
    dest.writeString(desc);
    dest.writeInt(status);
    dest.writeStringList(tags);
    dest.writeInt(is_spam ? 1 : 0);
    dest.writeInt(upvotes);
    dest.writeInt(downvotes);
    dest.writeInt(view_count);
    dest.writeString(uri_slug);
    dest.writeString(added_on);
    dest.writeString(user);
    dest.writeString(degree);
    dest.writeString(stream);
    dest.writeString(institute);
    dest.writeString(city);
    dest.writeString(state);
    dest.writeTypedList(answer_set);
}
}

这是答案类:

public class QnAAnswers implements Parcelable {
public static final Creator<QnAAnswers> CREATOR = new Creator<QnAAnswers>() {
    @Override
    public QnAAnswers createFromParcel(Parcel source) {
        return new QnAAnswers(source);
    }

    @Override
    public QnAAnswers[] newArray(int size) {
        return new QnAAnswers[size];
    }
};

public long id;
public String resource_uri;
public String question;
public String answer_text;
public int status;
public boolean is_spam;
public int upvotes;
public int downvotes;
public boolean best_answer;
public String added_on;
public String user;

public QnAAnswers() {}

public QnAAnswers(Parcel source) {
    id = source.readLong();
    resource_uri = source.readString();
    question = source.readString();
    answer_text = source.readString();
    status = source.readInt();
    is_spam = source.readString() == "true";
    upvotes = source.readInt();
    downvotes = source.readInt();
    best_answer = source.readString() == "true";
    added_on = source.readString();
    user = source.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(id);
    dest.writeString(resource_uri);
    dest.writeString(question);
    dest.writeString(answer_text);
    dest.writeInt(status);
    dest.writeInt(is_spam ? 1 : 0);
    dest.writeInt(upvotes);
    dest.writeInt(downvotes);
    dest.writeInt(best_answer ? 1 : 0);
    dest.writeString(added_on);
    dest.writeString(user);
}
}

我只能看到与QnAAnswers Class相关的字段,其他字段没有填充。

只有answer_set被填充

这是解析API响应并显示QnAFragment的活动中的代码:

    private void showQnAQuestions(String response) {
    try {
        String questions = extractResults(response);
        List<QnAQuestions> qnAQuestions = JSON.std.listOfFrom(QnAQuestions.class, questions);
        displayFragment(QnAQuestionsListFragment.newInstance(new ArrayList<>(qnAQuestions)), false);
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
}

    private String extractResults(String response) {
    try {
        //   JsonFactory jf = new JsonFactory();
        //Result r = Result.createFromJson(jf.createParser(json2));
        Map<String, Object> map = JSON.std.mapFrom(response);
        if (map.get("next") != null)
            next = map.get("next").toString();
        else
            next = null;
        return JSON.std.asString(map.get("results"));
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
    }
    return null;
}

任何指针都会非常有帮助!
1个回答

0

我注意到的第一件事是,您已经阅读:

is_spam = source.readString() == "true"

但是你需要将is_spam写成1或0并放入包裹中:

dest.writeInt(is_spam ? 1 : 0);

同样适用于最佳答案。


我在我的Java代码中注释掉了is_spam字段。但是问题仍然存在。 - Harsh Vardhan
最佳答案字段以及提到的内容。 - Tobias
已经完成了。我在QnAQuestions类中只保留字符串变量,但仍然没有得到结果。 - Harsh Vardhan
我尝试将变量设置为私有,但这也没有帮助我。还有其他什么我可以尝试吗? - Harsh Vardhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接