当前位置: 首页 > 面试题库 >

Android JSON CharSet UTF-8问题

沈英勋
2023-03-14
问题内容

我目前正在开发一个Android应用程序,该应用程序从提供JSON数据的API中获取其数据。我将JSON数据的项目存储为字符串,这导致出现一些奇怪的字符(例如’Â’)。我知道这与字符集有关,因此我将InputStreamReader设置为“
UTF-8”,但它似乎没有解决问题。

       URL hukd = new URL(requestUrl);
        URLConnection tc = hukd.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream(), "UTF-8"));
        String line = in.readLine();
        Log.d("line :", line);
        JSONObject obj = new JSONObject(line);
        JSONArray ja = obj.getJSONObject("deals").getJSONArray("items");
        for (int i = 0; i < ja.length(); i++) { // each deal
            JSONObject jo = (JSONObject) ja.get(i);
            // make the deal
            Deal d = new Deal(jo.getString("title"),
                    jo.getString("description"),
                    jo.getString("price"),jo.getString("temperature"), 
                    jo.getJSONObject("merchant").getString("name"),
                    jo.getString("deal_image"),
                    jo.getString("deal_image_highres"));
            listItems.add(d);
            Log.d("Deal:", d.toString());
        }

Log.d“行”给出

01-30 19:56:01.909: D/line :(610): {"deals":{"items":[{"title":"Absolute steal ** Harmony one remote in store only Comet \u00c2\u00a349.98 **","deal_link":"http:\/\/www.hotukdeals.com\/deals\/absolute-steal-harmony-one-remote-store-only-comet-49-98-1131779?aui=465","mobile_deal_link":"http:\/\/m.hotukdeals.com\/deals\/absolute-steal-harmony-one-remote-store-only-comet-49-98-1131779?aui=465","deal_image":"http:\/\/www.hotukdeals.com\/images\/threads\/1131779_1.jpg","description":"Was just in comet getting two new washing machines when I stumbled upon a load of Harmony ones on the end of an isle \u00e2\u0080\u00a6. I nearly blew a fuse when I saw they had been reduced from \u00c2\u00a380.00 to under \u00c2\u00a350 as I just bought 3 of these before xmas at \u00c2\u00a368.00 ea which I thought was a true bargain.\r\n\r\nI asked they guy and he said they were stopping doing them as far as he knew and that the deal should be nation wide this must be the  cheapest I've seen for the UK version ever  even though this is the Harmony one and not the one + is it still a great remote which does just about everything you could want it to. \r\n\r\n** note I don\u00e2\u0080\u0099t have a picture of the price tag but I have these at my home and are 100% that it\u00e2\u0080\u0099s the harmony one on sale hope this helps some of you guys  and girls out  **\r\n\r\nThe Logitech Harmony One remote control lets you replace 15 remotes with one, easy to use device. With its clear, touch screen display this Harmony learning remote control makes controlling the most complex of systems easy. A graphic interface allows you, for example, to watch a DVD at the touch of a button.\r\n","submit_time":"48 minutes ago","hot_time":"1 minute ago","poster_name":"no1son","temperature":106.88999939,"price":49.98,"timestamp":1327950390,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Audiovisual","url_name":"audiovisual"},"merchant":{"name":"Comet","url_name":"comet.co.uk"},"tags":{"items":[{"name":"leeds"}]},"deal_image_highres":"http:\/\/www.hotukdeals.com\/images\/threads\/high-res\/1131779_1.jpg"},{"title":"Youth Brazil Shirt \u00c2\u00a34 at Very\/Littlewoods","deal_link":"http:\/\/www.hotukdeals.com\/deals\/youth-brazil-shirt-4-very-littlewoods-1131765?aui=465","mobile_deal_link":"http:\/\/m.hotukdeals.com\/deals\/youth-brazil-shirt-4-very-littlewoods-1131765?aui=465","deal_image":"http:\/\/www.hotukdeals.com\/images\/threads\/1131765_1.jpg","description":"2010 Brazil shirt by Nike\r\nAvailable in all youth sizes.\r\nFree delivery through collect plus","submit_time":"1 hour, 4 minutes ago","hot_time":"14 minutes ago","poster_name":"ericagradus","temperature":161.479995728,"price":4,"timestamp":1327949447,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Fashion","url_name":"fashion"},"merchant":{"name":"Very","url_name":"very.co.uk"},"tags":{"items":[{"name":"brazil shirt"},{"name":"very"},{"name":"littlewoods"}]},"deal_image_highres":"http:\/\/www.hotukdeals.com\/images\/threads\/high-res\/1131765_1.jpg"},{"title":"Milk Chocolate Mikado 29p at Home Bargains","deal_link":"http:\/\/www.hotukdeals.com\/deals\/milk-chocolate-mikado-29p-home-bargains-1130742?aui=465","mobile_deal_link":"http:\/\/m.hotukdeals.com\/deals\/milk-chocolate-mikado-29p-home-bargains-1130742?aui=465","deal_image":"http:\/\/www.hotukdeals.com\/images\/threads\/1130742_1.jpg","description":"Was in Home Bargains in Lancaster and they had the MIlk Chocolate Mikado for 29p. The white and hazelnut were 59p. Didn't check the sell by day as it took all my will power to resist haha!\r\n\r\nSorry if this is a duplicate I did look for it but couldn't find it.","submit_time":"1 day, 3 hours ago","hot_time":"14 minutes ago","poster_name":"cazi77","temperature":146.61000061,"price":0.29,"timestamp":1327853963,"expired":"false","forum":{"name":"Deals","url_name":"deals"},"category":{"name":"Groceries","url_name":"groceries"},"merchant":{"name":"Home Bargains","url_name":"homebargains.co.uk"},"tags":{"items":[]},"deal_image_highres":"http:\/

有任何想法吗?


问题答案:

尝试以下代码片段代码,当我遇到类似问题时该代码对我有帮助:

new String(jo.getString("name").getBytes("ISO-8859-1"), "UTF-8");


 类似资料:
  • 嗨,我试图使我在UTF-8兼容的应用程序之一。我的环境如下:linux操作系统,apahce网络服务器作为超文本传输协议监听器,tomcat作为servlet引擎 配置了mod_jk和tomcat的apache s使用ajp连接器。 我已经从少数网站上阅读了UTF-8的基本指南,并根据建议尝试了以下方法 为服务器中的连接器设置和。xml 设置语言bashrc/. file使用 将apache服务器

  • 我不能让UTF-8在Tomcat中工作。我已经完成了以下配置: 在我的server.xml上设置URIEncode="UTF-8" 导出CATALINA_OPTS=$CATALINA_OPTS-Dfile.encoding=UTF-8catalina.bat 配置Spring特性EncodingFilter 在所有html文件中设置UTF-8 Maven来源编码构建UTF-8 当我在浏览器或邮递员

  • 问题内容: 使用UTF-8编码创建字符串时存在不一致的地方。 运行此代码: 在Java 1.8.0_20(及更早版本)上,我们得到结果 在Java 1.7和1.6上,我们得到正确的结果: 您遇到此错误了吗?有没有解决方法? 对于Shift_JIS,JIS_X0212-1990,x-IBM300,x-IBM834,x-IBM942,x-IBM942C,x-JIS0208来说,这种不一致也很明显,但显

  • 亲爱的读者,这些Java 8 Interview Questions专门设计用于让您熟悉在面试Java 8 Language时可能遇到的问题的本质。 根据我的经验,很好的面试官在你的面试中几乎不打算问任何特定的问题,通常问题从这个主题的一些基本概念开始,然后他们继续基于进一步的讨论和你回答的问题 - JAVA 8中引入了哪些新功能? Java 8中添加了许多功能,最重要的功能如下所述 - Lamb

  • 问题内容: 我一直使用ISO-8859-1编码,但是现在要转到UTF-8。 不幸的是我无法正常工作。 我的MySQL数据库是UTF-8,我的PHP文档是用UTF-8编码的,我设置了UTF-8字符集,但仍然无法使用。 (像æ/ø/å这样的特殊字符不起作用) 希望你们能提供帮助! 问题答案: 确保与数据库的连接也使用此字符集: 根据php.net 的文档: 另请参见:http : //nl3.php.

  • 问题内容: 我从网页上获取的JSON数组出现奇怪的字符编码问题。服务器正在发回此标头: 内容类型文字/ javascript;字符集= UTF-8 另外,我可以查看Firefox或任何浏览器中的JSON输出,并且Unicode字符正确显示。响应有时会包含来自其他语言的带有重音符号等的单词。但是,当我将其下拉并放在Java中的字符串中时,我得到了那些奇怪的问号。这是我的代码: 如您所见,我在Inpu