【问题标题】:Parse nested custom object array using jackson and parcelable使用 jackson 和 parcelable 解析嵌套的自定义对象数组
【发布时间】:2015-08-13 08:10:07
【问题描述】:

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

这是 Activity 中解析 A​​PI 响应并显示 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;
}

任何指针都会有很大帮助!

【问题讨论】:

    标签: java android json jackson parcelable


    【解决方案1】:

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

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

    但是你将 is_spam 作为 1 或 0 写入 Parcel:

    dest.writeInt(is_spam ? 1 : 0);
    

    最佳_答案相同。

    【讨论】:

    • 我在我的 java 代码中注释掉了 is_spam 字段。同样的问题仍然存在。
    • best_answer 字段以及提及。
    • 做到了。我只在 QnAQuestions 类中保留字符串变量。仍然没有得到结果。
    • 我尝试将变量设为私有,但这对我也没有帮助。我还可以尝试其他什么方法??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多