2012-08-06 17 views
0

我正在試圖製作一個需要訪問一些webservices,以JSon格式返回數據的Android應用程序。如何使用Gson(Android)解析html元素?

我正在使用Gson來解析這些數據,從來沒有任何問題,這個庫。

事情顯然是一個web服務返回給我一個字符串與HTML標籤(實際上,從HTML標籤開始)。問題是,即使我在解析字符串時沒有異常,這個特定的字段在輸出中看起來是空的。

JSON的我試圖解析如下所示:

{ 
    "ID": "66", 
    "Nom": "Bordeaux", 
    "Pays": "FRANCE", 
    "Titre_office": "Office de Tourisme de", 
    "Descriptif_office": "<a href=\"http://www.secure-hotel-booking.com/office-de-tourisme-de-bordeaux/2T66/dateselection?sid=1811faff-feda-4f98-83b5-89d6eb6266e8\">Disponibilités des hébergements en ville</a> Bordeaux, ville riche en histoires et en lieux, avec son fabuleux patrimoine mondial, sa culture, sa gastronomie et son art de vivre, vous accueille toute l'année pour profiter d'un excellent moment.", 
    "Logo_office": "ville_66.jpg", 
    "Adresse_office": "blabla", 
    "Num_contact": "+33xxxxxxxxxx", 
    "Mail_contact": "[email protected]", 
    "Site_contact": "http://www.bordeaux-tourisme.com" 
} 

當然,所有的字段都ok,除了「Descriptif_office」之一。我嘗試使用新的GsonBuilder()。disableHtmlEscaping()。create();我嘗試使用新的GsonBuilder()。disableHtmlEscaping()。但與嚴格沒有effet ...

任何幫助將不勝感激!

感謝

編輯:

我使用硬編碼字符串時,都沒有問題:

new GsonBuilder().disableHtmlEscaping().create().fromJson(new StringReader("   {\"Descriptif_office\":\"<a href=\\\"http:\\/\\/www.secure-hotel-booking.com\\/office-de-tourisme-de-bordeaux\\/2T66\\/dateselection?sid=1811faff-feda-4f98-83b5-89d6eb6266e8\\\">Disponibilit\\u00e9s des h\u00e9bergements en ville<\\/a> Bordeaux, ville riche en histoires et en lieux, avec son fabuleux patrimoine mondial, sa culture, sa gastronomie et son art de vivre, vous accueille toute l'ann\u00e9e pour profiter d'un excellent moment.\"}"), test.class); 

回答

0

總是檢查你是否調用了正確的web服務。愚蠢的我。

0

嘗試使用JSONString.replace("htmlYouWantToEspace", "whatYouWantToInstert");。這樣你可以避免問題。

+0

那麼,理想的解決方案是讓我保存所有數據。我不想轉義html鏈接,我想獲取並稍後在webview中使用它... – Ours 2012-08-06 15:13:47

+0

如果您使用Android的Linkify,仍然可以使用它們。 – tolgap 2012-08-06 15:32:26