2013-10-20 3 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());  

, 나는 여기에 어떤 시도/catch 블록을 무시했다. 이렇게하면 사용자가 입력 한 텍스트를 캡처하고 표시하는 방법에 대한 아이디어를 얻을 수 있습니다.

+0

어쩌면 듭니다 귀하의 문자 인코딩에 문제가 있습니다. 코드는 어떻게 생겼습니까? – hakanostrom

답변

5

이러한 종류의 특수 문자를 사용하려면 "UTF-8"을 사용해야합니다.

URLEncoder.encode("Your Special Character", "UTF8"); 

당신은 여기에서이 문제에 대해 비슷한 질문을 확인할 수 있습니다 : 자세한 내용은 http://developer.android.com/reference/java/nio/charset/Charset.html

읽기 위해이 방법처럼 예상 문자를 인코딩해야

Android: Parsing special characters (ä,ö,ü) in JSON

+0

UTF-8 인코딩으로 문제가 해결되었습니다. 고마워. – user1938357

+0

줄 바꿈 문자 '\ n'에 대해 작동하지 않습니다. –