如何处理JSON字符串特殊字符 [英] How to handle special characters in json string

查看:190
本文介绍了如何处理JSON字符串特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我的用户输入的文本存储到一个JSON字符串。然后,我存储JSON字符串到一个文件中。再后来就被读取文件,提取它的JSON字符串,最终让显示到一个TextView的字符串显示了回去。不过,我注意到,任何特殊字符(而不是符号),如£,昆明,€等不显示了。例如符号€被显示为□¬。

I have an app where I store user entered text into a json string. I then store that json string into a file. And then later on display it back by reading the file, extracting the json string from it and finally getting the string to display into a textview. I am however noticing that any special characters(rather symbols) like £, ÷, € etc are not displayed back. For example the symbol € gets displayed as â□¬.

下面的一些示例code仅供参考

Some sample code below for reference

首先,code捕捉用户输入的文本,并把它变成一个文件

First the code for capturing user entered text and putting it into a file

//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();

现在低于$ C $下获取字符串后面的文件,并显示给用户。

Now below the code for getting the string back from file and displaying it to user

//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());       

在code以上仅仅是一个样品,我在这里忽略任何try / catch块。这应该给关于我如何捕捉和显示用户输入文本的想法。

The code above is just a sample, I have ignored any try/catch blocks here. This should give an idea about how I am capturing and displaying the user entered text.

推荐答案

您必须使用UTF-8使用这种特殊的性质。有关详细信息阅读:

You have to use "UTF-8" for using this kind of special character. For details read this :

http://developer.android.com/reference/java/ NIO /字符集/ Charset.html

您必须连接code你喜欢这样期望的字符:

You have to encode for your expected character like this way :

URLEn coder.en code(你的特殊字符,UTF8);

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

您可以检查类似的问题,这个问题在这里:

You can check similar question about this issue from here :

<一个href="http://stackoverflow.com/questions/5536140/android-parsing-special-characters-a-o-u-in-json">Android:解析特殊字符(ä,ö,ü)的JSON

这篇关于如何处理JSON字符串特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆