【发布时间】:2015-04-29 03:14:07
【问题描述】:
每当我解析JSON字符串并在android Logcat中显示json输出的输出时,我总是发现
"f������a������m������i������l������y������.��� ���p������n������g"
在 Logcat 中。实际的字符串是“family.png”,但显示如上。知道如何解决这个问题吗?
这是 JSON 数据。
{"members": [
{"user":"���d���e���8���8���f���5���c���7���3���7���1���4���7���6���6���f", "username":"���P���P���S���h���e���i���n", "avatar":"f������a������m������i������l������y������.������p������n������g"}
]}
并由 Coldfusion 生成 JSON 格式。
{"members": [
<cfoutput query="getQry">
{"user":"#tuser#", "username":"#tusername#", "avatar":"#tpicture#"} <cfif currentrow LT getTrackQry.recordcount>,</cfif>
</cfoutput>
]}
这里是从 URL 解析 JSON 对象。
public static JSONObject getFromUrl(String url) {
InputStream is = null;
String content = null;
JSONObject jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
content = sb.toString();
} catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(content);
} catch (JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
【问题讨论】:
-
你控制 JSON 数据吗?你自己生成吗?显示使用代码。向我们展示 JSON。
-
是的,我自己生成了 JSON 数据并在我的问题中添加了 JSON 数据。
-
@Pang 我知道。这就是为什么我要问为什么“family.png”会错成“f������a������m������i������l������ android logcat 中的“y������.������p������n������g”。
-
@ppshein 好的,我们很接近了。贴出你用来解析它的代码。
-
new BufferedReader(new InputStreamReader(is,"utf8"),8)应该是new BufferedReader(new InputStreamReader(is, "UTF-8")),请参阅stackoverflow.com/questions/6511880/…。