Android parse special characters in json response

微笑、不失礼 提交于 2019-12-10 16:00:36

问题


In my Android application I get JSON response string from a PHP url. from the response I get some hotel names with apostrophe, I get &#039 character instead of apostrophe. How can I parse the hotel with special characters in android? I can see the apostrophe in the browser but could not see in android logcat.

I have tried jresponse = URLEncoder.encode(jresponse,"UTF-8"); but I could not get apostrophe for hotel name.

This is the one of the hotel name in the response.

 I see the following in browser.

    {"id":12747,
     "name":"Oscar's",
     ....
    }
But in the logcat:

     id 12747
     name Oscar's

回答1:


Use the decoder instead of encoder. URLDecoder.decode(jresponse,"UTF-8")




回答2:


Use ISO-8859-2 when you create the URLEncodedEntity that you send off. You can set this as a parameter in the constructor.

Without a specified charset, you are probably sending the data in UTF-8/UTF-16 (most common) which the server is interpreting in a different way.

EDIT: It looks like ISO-8859-2 doesn't support ñ. You may have to change something server-side. http://en.wikipedia.org/wiki/ISO/IEC_8859-2




回答3:


You can try Html class. eg :- jresponse = Html.fromHtml(jresponse);



来源:https://stackoverflow.com/questions/22301169/android-parse-special-characters-in-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!