2013-03-04 49 views
0

這並不轉換爲å轉換爲國際字符不使用jsonobject.tostring,但與字符串文字?

String j_post = new String((byte[]) j_posts.getJSONObject(i).get("tagline").toString().getBytes("utf-8"), "utf-8"); 

但下面確實

String j_post = new String((byte[]) "\u00e5".getBytes("utf-8"), "utf-8"); 

如何解決這個問題?

更新:現在我試圖修復編碼之前,我把它作爲JSONObject,它仍然無法正常工作。

json = new JSONObject(new String((byte[]) jsonContent.getBytes("utf-8"), "utf-8")); 

    JSONArray j_posts = json.getJSONArray("posts"); 
    for (int i = 0; i<j_posts.length();i++){ 
     //[String(byte[] data)][2] 
     String j_post =j_posts.getJSONObject(i).get("tagline").toString(); 
     post_data.add(new Post(j_post)); 
    } 

請注意,我收到一個字符串作爲我的網絡服務器的響應。

回答

1

這是因爲您的JSON沒有所需格式的字符。查看JSON準備好的代碼,並在其中包含UTF-8編碼,當JSON形成時。

+0

我嘗試了你的建議,但它仍然無法正常工作。 – 2013-03-04 14:40:09

+0

我在提交數據時出錯。雖然我不能讓我的代碼工作,但這似乎是一個合理的答案。謝謝你的時間。我會更新這個線程,如果我找出什麼是錯的。 – 2013-03-04 19:43:03

0
String j_post = new String((byte[]) "\u00e5".getBytes("utf-8"), "utf-8"); 

的確是(更好):

String j_post = "\u00e5"; 

,因此

String j_post = new String((byte[]) j_posts.getJSONObject(i).get("tagline") 
     .toString().getBytes("utf-8"), "utf-8"); 

String j_post = j_posts.getJSONObject(i).get("tagline").toString(); 

所以@RJ是正確的,數據是錯位:要麼在獲取或發送(錯誤的編碼)。