0

这不会转换为å

String j_post = new String((byte[]) j_posts.getJSONObject(i).get("tagline").toString().getBytes("utf-8"), "utf-8");

但以下确实

String j_post = new String((byte[]) "\u00e5".getBytes("utf-8"), "utf-8");

我该如何解决?

更新:现在我尝试在将其转换为 JSONObject 之前修复编码,但它仍然不起作用。

json = new JSONObject(new String((byte[]) jsonContent.getBytes("utf-8"), "utf-8"));

    JSONArray j_posts = json.getJSONArray("posts");
    for (int i = 0; i<j_posts.length();i++){
        //[String(byte[] data)][2]
        String j_post =j_posts.getJSONObject(i).get("tagline").toString();
        post_data.add(new Post(j_post));
    }

请注意,我从我的网络服务器收到一个字符串作为响应。

4

2 回答 2

1

这是因为您的 JSON 没有所需格式的字符。UTF-8当 JSON 形成时,查看准备 JSON 的代码并在其中包含编码。

于 2013-03-04T14:08:20.873 回答
0
String j_post = new String((byte[]) "\u00e5".getBytes("utf-8"), "utf-8");

确实是(更好):

String j_post = "\u00e5";

因此

String j_post = new String((byte[]) j_posts.getJSONObject(i).get("tagline")
        .toString().getBytes("utf-8"), "utf-8");

String j_post = j_posts.getJSONObject(i).get("tagline").toString();

所以@RJ 是对的,并且数据被破坏:无论是获取还是发送(错误的编码)。

于 2013-03-04T14:51:56.600 回答