2012-01-30 2 views
16

현재 Android 애플리케이션에서 작업 중이며, JSON 데이터를 제공하는 API에서 데이터를 가져옵니다. JSON 데이터의 항목을 String으로 표시하여 이상한 문자가 표시됩니다 (예 : 'Â'). 이 문제는 문자 집합과 관련되어 있으므로 UTF-8로 InputStreamReader를 설정했지만 문제가 해결되지 않은 것 같습니다.Android JSON CharSet 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:\/ 

어떤 아이디어를 준다?

+0

"ISO-8859-1"과 같은 다른 인코딩을 사용해 보셨습니까? 나는 이것을 사용하여 몇 가지 문제를 해결했습니다 ... 그러나 그것은 악센트 부호가있는 글자들이었습니다. 어떤 문자들이 당신에게이 문제들을주고 있습니까? – thermz

+0

Log.d ("line :", line)의 결과는 정확히 무엇입니까? – Jens

+0

죄송합니다 로그 출력을 넣어 주신 것을 잊었습니다 (Jens에게 감사). – bencallis

답변

39

나는 비슷한 문제 가진 때 나에게 도움을 준 다음 코드 코드보십시오 :이 후 몇 년 알고

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

고맙습니다. 이 작품! – bencallis

+1

왜 그런 꼬인 함수를 사용해야합니까? try catch 블록으로 묶어야합니다. –

+1

저는 개인적으로 모든 json 파싱을 try catch 블록 안에 넣으려고합니다. – vm204

0

을,하지만 난 이것보다 더 신뢰할 수있는 솔루션을 마련했습니다 하나, 그리고 내가 다른 뭔가를 찾고있는 동안이이 사고에 의해 발견 된

인코딩 여분의 물건을 모두 필요로하지 않지만, 작동이 난을 무엇

"" + jo.opt("name"); 

을 결과를 원시 객체로 얻고, 빈 문자열의 끝 부분에 넣으면 그것을 문자열 형식으로 만든다. 물론 원래의 문자열이 아닌 문자열 일 경우, 결과가 흐릿해질 수 있으므로, 첫 번째 문자열

나는 이런 식으로 뭔가가 :

String s = ""; 
try { 
    s = jo.getString("name"); 
    s = "" + jo.opt("name"); 
} catch (Exception e) { 
    Log.e("JSON error", "error:" + e.toString()); 
} 

이 문자열이 존재 테스트 한 다음

내가이 미래 :-) 누군가 도움이되기를 바랍니다 완전히 인코딩 된 버전으로 대체됩니다