2012-08-06 88 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