使用jackson和parcelable解析嵌套的自定义对象数组

时间:2015-08-13 08:10:07

标签: java android json jackson parcelable

我正在尝试使用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相关的字段。未填充所有其他字段。

Only answer_set is Populated

这是活动中的代码,用于解析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 :(得分:0)

我注意到的第一件事是,你读到了:

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

但您将is_spam写为1或0到Parcel:

dest.writeInt(is_spam ? 1 : 0);

同样的最佳答案。