解決java模擬get請求,獲取json數據,簡單解析,再暴露get請求時,中文出現亂碼的問題。

public static JSONObject parseTargetJson(JSONObject jsonObject) {
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("result");
        for (Object object : jsonArray) {
            JSONObject subJson = JSONObject.parseObject(object.toString());
            //重新按UTF-8的方式進行編碼
            String key = new String(subJson.getJSONObject("metric").getString("title").getBytes(), Charset.forName("UTF-8"));
            jsonObject1.put(key, subJson.getJSONArray("value").get(1));

        }
        return jsonObject1;
    }

哪裏出現中文字符亂碼,就用UTF-8重新進行一次編碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章