2013-07-21 43 views
1

我一直在努力使用jackson Json Parser解析此feed。使用Jackson Json解析Google博客帖子

這裏是飼料:

{ 
"responseData": { 
"query": "Official Google Blogs", 
"entries": [ 
{ 
"url": "http://googleblog.blogspot.com/feeds/posts/default", 
"title": "<b>Official Blog</b>", 
"contentSnippet": "5 days ago <b>...</b> <b>Official</b> weblog, with news of new products, events and glimpses of life inside <br> <b>Google</b>.", 
"link": "http://googleblog.blogspot.com/" 
}, 
{ 
"url": "http://googlewebmastercentral.blogspot.com/feeds/posts/default", 
"title": "<b>Official Google</b> Webmaster Central <b>Blog</b>", 
"contentSnippet": "Jul 12, 2013 <b>...</b> The <b>official</b> weblog on <b>Google</b> crawling and indexing, and on webmaster tools, <br> including the Sitemaps facility.", 
"link": "http://googlewebmastercentral.blogspot.com/" 
}, 

{ 
"url": "http://googlemac.blogspot.com/feeds/posts/default", 
"title": "<b>Official Google</b> Mac <b>Blog</b>", 
"contentSnippet": "Jun 22, 2012 <b>...</b> The <b>official</b> weblog about <b>Google</b> in the Apple Macintosh world.", 
"link": "http://googlemac.blogspot.com/" 
} 
] 
}, 
"responseDetails": null, 
"responseStatus": 200 
} 

我設法得到除「條目」數組反序列化的所有對象。

任何想法?

這裏是我的數據模型代碼:

public class UserModel { 

public static final String TAG = UserModel.class.getSimpleName() 


public static class ResponseData{ 
    private String _query; 
    public String getQuery(){return _query;} 
    public void setQuery(String query){_query=query;} 
    // I am unable to get the entries parsing 


} 



private String responseStatus; 
private String responseDetails; 
private ResponseData responseData; 


public ResponseData getresponseData() { 
    return responseData; 
} 



public void setresponseData(ResponseData responseData) { 
     this.responseData = responseData; 
    } 


    public String getresponseStatus() { 
     return responseStatus; 
    } 

    public void setresponseStatus(String responseStatus) { 
     this.responseStatus = responseStatus; 
    } 
    public String getresponseDetails() { 
     return responseDetails; 
    } 

    public void setresponseDetails(String responseDetails) { 
     this.responseDetails = responseDetails; 
    } 


    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("ResponseStatus [ResponseStatus="); 
     builder.append(responseStatus); 
     builder.append(responseData._query); 
     builder.append("]"); 
     return builder.toString(); 
    } 

} 

回答

0

您需要將條目列表作爲List類型添加到您的responseData類中,並且還要創建一個表示Entry類的類(在下面的示例中稱爲entry,但可以隨意調用它)。

public static class ResponseData{ 

    public List<Entry> entries; 
    private String _query; 
    public String getQuery(){return _query;} 
    public void setQuery(String query){_query=query;} 
    // I am unable to get the entries parsing 


} 

編輯: 也 - 確保您的Entry類包含JSON響應特性。它應該有一個URL,標題,contentSnippet和鏈接屬性。

+0

Hi-thats great。一旦我添加了列表類型,然後如何使用Getters和setter像其他人一樣填充它們? –

+0

那麼如果你把它公開,jackson會自動填充對象條目。事實上,在android中,使用公共成員來創建模型對象並不罕見(因爲getter和setter被認爲是更昂貴的 - 儘管谷歌的推薦很久以前就已經發布了......)。如果你希望它跟其他成員一樣,那麼就把它設爲私有,並添加一個getter和setter(分別爲getEntries,setEntries)--Jack仍然應該選擇這個屬性並根據JSON進行設置。 – Eggman87

+0

@androidbloke有什麼好運? – Eggman87

0

我不知道我是否正確地理解你的問題。

JSONObject data = new JSONObject(query); 
JSONObject responseData = data.getJSONObject("responseData"); 
JSONArray entries = responseData.getJSONArray("entries"); 
List<JSONObject> pEntries = new ArrayList<JSONObject>(); 
for(int i = 0; i < entries.length(); i++){ 
    pEntries.add(entries.getJSONObject(i)); 
} 

用於從JSONObject獲取字符串調用getString(「name」);

+0

嗨我是在使用Jackson Library這樣做之後 –