Android 如何处理json字符串中的特殊字符

Android 如何处理json字符串中的特殊字符,android,json,character-encoding,Android,Json,Character Encoding,我有一个应用程序,将用户输入的文本存储到json字符串中。然后,我将该json字符串存储到一个文件中。然后稍后通过读取文件、从中提取json字符串并最终将字符串显示到textview中来显示它。但我注意到,任何特殊字符(而非符号)如、÷、€等都不会显示出来。例如,符号€显示为–□嗯 下面是一些示例代码供参考 首先是用于捕获用户输入的文本并将其放入文件的代码 //get user entered text QuestionEditText = (EditText)this.findViewById

我有一个应用程序,将用户输入的文本存储到json字符串中。然后,我将该json字符串存储到一个文件中。然后稍后通过读取文件、从中提取json字符串并最终将字符串显示到textview中来显示它。但我注意到,任何特殊字符(而非符号)如、÷、€等都不会显示出来。例如,符号€显示为–□嗯

下面是一些示例代码供参考

首先是用于捕获用户输入的文本并将其放入文件的代码

//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块。这应该可以让我了解如何捕获和显示用户输入的文本。

使用这种特殊字符时,必须使用“UTF-8”。详情请参阅

您必须按照以下方式为预期角色编码:

URLEncoder.encode("Your Special Character", "UTF8");
您可以在此处查看有关此问题的类似问题:

要使用这种特殊字符,必须使用“UTF-8”。详情请参阅

您必须按照以下方式为预期角色编码:

URLEncoder.encode("Your Special Character", "UTF8");
您可以在此处查看有关此问题的类似问题:


可能是您的字符编码有问题。你的代码是什么样子的?可能是你的字符编码有问题。你的代码看起来像什么?