2015-08-13 69 views
3

我想使用Jackson解析JSON到Java Bean。使用jackson和parcelable解析嵌套的自定義對象數組

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; 
} 

任何指針會做很大的幫助!

回答

0

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

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

,但你寫is_spam爲1或0到地塊:

dest.writeInt(is_spam ? 1 : 0); 

同爲最佳答案_。

+0

我在我的java代碼中註釋掉了is_spam字段。同樣的問題依然存在。 –

+0

best_answer字段以及提及。 – MasterCassim

+0

做到了。我只在QnAQuestions類中保留字符串變量。仍然沒有得到結果。 –