Java Android JSON解析磅符号将作为?
我有一个JSON文件,其中包含一个英镑符号。此json是从手机上临时存储的文件中提取的。它将来自服务器,但目前我只是将其存储在手机上。我将JSON文件转换为输入流,并使用方法将其转换为如下字符串Java Android JSON解析磅符号将作为?,java,android,Java,Android,我有一个JSON文件,其中包含一个英镑符号。此json是从手机上临时存储的文件中提取的。它将来自服务器,但目前我只是将其存储在手机上。我将JSON文件转换为输入流,并使用方法将其转换为如下字符串 Resources res = getResources(); String sJsonVariables = iStream_to_String(res .openRawResource(R.raw.widgvariables)); byte []b = sJsonV
Resources res = getResources();
String sJsonVariables = iStream_to_String(res
.openRawResource(R.raw.widgvariables));
byte []b = sJsonVariables.getBytes();
String s1 = new String(b, "UTF-8");
String s2 = new String(b, "ASCII");
String s3 = new String(b, "Cp1252");
String s4 = new String(b, "ISO8859_1");
iStream_to_String方法如下
public String iStream_to_String(InputStream is) {
BufferedReader rd = new BufferedReader(new InputStreamReader(is), 4096);
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
我已经尝试使用Log.v打印json,以测试它是否打印正常。除了英镑符号外,所有东西都打印得很好。我尝试了几种不同的编码方式
Resources res = getResources();
String sJsonVariables = iStream_to_String(res
.openRawResource(R.raw.widgvariables));
byte []b = sJsonVariables.getBytes();
String s1 = new String(b, "UTF-8");
String s2 = new String(b, "ASCII");
String s3 = new String(b, "Cp1252");
String s4 = new String(b, "ISO8859_1");
然而,没有人会打印出英镑符号,它要么是黑色问号,要么是其他奇怪的符号。有人知道我做错了什么,以及我如何才能正确打印出这个符号吗?将它与try-catch
byte[]b=sJsonVariables.getBytes(“UTF8”)一起使用代码>和字符串s3=新字符串的字符串(b,“Cp1252”);
不,不幸的是,同样的事情也发生了。第一个UTF-8编码样式使用该方法返回黑色问号,其他的只是将其全部删除。使用该方法的计算机使用Cp1252编码,现在工作正常。您永远不应该使用getBytes(ENCODING1)
和新字符串(ENCODING2)
。这就像说“用法语给我这个词,然后假装它是英语”。完全是白痴,但经常被那些不知道编码是如何工作的人暗示。