2013-10-20 143 views
0

我有一個應用程序,用於將用戶輸入的文本存儲到json字符串中。然後我將該json字符串存儲到一個文件中。然後通過讀取文件顯示回來,從中提取json字符串,最後將字符串顯示爲textview。然而,我注意到任何特殊字符(而不是符號),例如£,÷,€等都沒有顯示出來。例如,符號€顯示爲‬。如何處理json字符串中的特殊字符

下面一些示例代碼,以供參考

首先用於捕捉用戶輸入的文本,並把它變成一個文件現在

//get user entered text 
QuestionEditText = (EditText)this.findViewById(R.id.editTextQuestion); 
//put that into json object 
JSONObject jObjOneQuestionDetails=new JSONObject(); 
jObjOneQuestionDetails.put("Question", QuestionEditText.getText()); 
//write json object into file 
FileOutputStream output = openFileOutput("MyFileName",MODE_PRIVATE); 
OutputStreamWriter writer = new OutputStreamWriter(output); 
writer.writejObjOneQuestionDetails.put()); 
writer.flush(); 
writer.close(); 

下面的代碼從文件中獲取字符串回來了,它顯示的代碼用戶

//define and initialize variables 
QuestionEditText = (EditText)this.findViewById(R.id.editTextQuestion); 
private JSONArray jArrayQuizQuestions=new JSONArray(); 
private JSONObject jObjQuizTitle=new JSONObject(); 

//load JSONObject with the File 
int ch; 
StringBuffer fileContent = new StringBuffer(""); 
FileInputStream fis; 
String fileString; 
fis = this.getBaseContext().openFileInput("MyFileName"); 
while((ch = fis.read()) != -1) 
      fileContent.append((char)ch); 
fileString = new String(fileContent); 
jObjQuizTitle = new JSONObject(fileString); 
jArrayQuizQuestions = jObjQuizTitle.getJSONArray("MyFileName"); 


//display json object into textview 
JSONObject aQues = jArrayQuizQuestions.getJSONObject(pageNumber-1); 
String quesValue = aQues.getString("Question"); 
QuestionEditText.setText(quesValue.toCharArray(), 0, quesValue.length());  

上面的代碼只是一個示例,我忽略了任何try/catch塊。這應該給我一個關於如何捕獲和顯示用戶輸入的文本的想法。

+0

也許it'sa問題與您的字符編碼。你的代碼是什麼樣的? – hakanostrom

回答