2012-01-30 7 views
16

मैं वर्तमान में एक एंड्रॉइड एप्लिकेशन पर काम कर रहा हूं, जो एक एपीआई से अपना डेटा प्राप्त करता है जो JSON डेटा प्रदान करता है। मैं JSON डेटा के लिए स्ट्रिंग्स के रूप में आइटम संग्रहीत कर रहा हूं जिसके परिणामस्वरूप कुछ अजीब चरित्र दिख रहे हैं (उदा। 'Â')। मैं इसे चरित्र सेट के साथ करने के लिए समझता हूं, इसलिए मैंने इनपुटस्ट्रीम रीडर को "यूटीएफ -8" पर सेट किया है, लेकिन ऐसा लगता है कि यह समस्या हल नहीं हुई है।एंड्रॉइड 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

क्या आपने "आईएसओ -885 9 -1" जैसे अन्य एन्कोडिंग की भी कोशिश की है? मैंने इसका उपयोग करके कुछ मुद्दा हल किया ... हालांकि यह उच्चारण अक्षरों के लिए था .. कौन से पात्र आपको यह समस्याएं दे रहे हैं? – thermz

+0

Log.d ("line:", line) का आउटपुट वास्तव में क्या है? – Jens

+0

क्षमा करें लॉग आउटपुट (धन्यवाद जेन्स) डालना भूल गया। – bencallis

उत्तर

39

निम्नलिखित स्निपेट कोड जो मुझे मदद की है जब मैं एक ऐसी ही समस्या हो रही थी प्रयास करें:

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

बहुत बहुत धन्यवाद। यह काम! – bencallis

+1

हमें ऐसे मोड़ वाले फ़ंक्शन का उपयोग क्यों करना है? इसे एक कोशिश पकड़ ब्लॉक में संलग्न किया जाना है। –

+1

मैं व्यक्तिगत रूप से मामले में बस पकड़ने के ब्लॉक के अंदर अपने सभी जेसन पार्सिंग को संलग्न करना पसंद करता हूं। – 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()); 
} 

यह परीक्षण किया जाएगा स्ट्रिंग मौजूद है, और फिर पूरी तरह से इनकोडिंग संस्करण

मुझे आशा है कि यह भविष्य :-) में किसी को मदद करता है के साथ बदलना

संबंधित मुद्दे