java JSON 文本编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15515459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:53:17  来源:igfitidea点击:

java JSON text encoding issue

javajsoncharacter-encodinggoogle-search-appliancewindows-1252

提问by Hari Reddy

In my application I retrieve search results in JSON format from an external tool called Google Search Appliance(GSA).

在我的应用程序中,我从名为 Google Search Appliance (GSA) 的外部工具检索 JSON 格式的搜索结果。

The JSON result from GSA is very large and therefore I prefer to modify the GSA JSON result into something more suitable for displaying on my webpage.

GSA 的 JSON 结果非常大,因此我更喜欢将 GSA JSON 结果修改为更适合在我的网页上显示的内容。

If I directly display the GSA JSON result without formatting it in my java code I'm not facing any encoding issues on my webpage.

如果我直接显示 GSA JSON 结果而不在我的 Java 代码中对其进行格式化,那么我的网页上不会遇到任何编码问题。

But if I format the large GSA JSON result into a suitable JSON format in my servlet java code I'm facing encoding problems. Example - “All Access Pass”gets displayed as ?All Access Pass?.

但是,如果我在我的 servlet java 代码中将大型 GSA JSON 结果格式化为合适的 JSON 格式,我将面临编码问题。示例 - “All Access Pass”显示为?All Access Pass?.

I return the modified json from my servlet to the webpage use the following code -

我使用以下代码将修改后的 json 从我的 servlet 返回到网页 -

response.setContentType("application/json;charset=UTF-8");

I have tried to change the charset to iso-8859-1but it does not make any difference.

我试图将字符集更改为,iso-8859-1但没有任何区别。

I edit my original JSON in the following manner -

我按以下方式编辑我的原始 JSON -

        String responseText = getMethod.getResponseBodyAsString();

        JSONObject resultJSON = new JSONObject();
                try {

                    JSONObject jsonObj = new JSONObject(responseText);

                    JSONArray resultJsonArray = jsonObj
                            .getJSONArray("RES");

                    JSONObject searchResultJSON = null;

                    for (int iCnt = 0; iCnt < resultJsonArray.length(); iCnt++) {

                        searchResultJSON = new JSONObject();

                        JSONObject obj = resultJsonArray.getJSONObject(iCnt);
                        JSONObject metaTagObj = obj
                                .getJSONObject("MT");

                        if (metaTagObj.has(("title"))) {
                         searchResultJSON.put("title",metaTagObj.get("title").toString());
                        }
             resultJSON.accumulate("RES", searchResultJSON);
    }
   response.setContentType("application/json;charset=UTF-8"); 
   response.getWriter().print(resultJSON);

    }catch(JSONException e){}

The modification to the original JSON which I'm going here can be done in JavaScript which would solve my problem but it is something which I do not want to do.

我要在这里修改原始 JSON 可以在 JavaScript 中完成,这将解决我的问题,但这是我不想做的事情。

  1. Is there a way to find out the encoding format of the text in the original GSA JSON?
  2. How can I avoid the java code from changing the text encoding in the original GSA JSON?
  1. 有没有办法找出原始GSA JSON中文本的编码格式?
  2. 如何避免 Java 代码更改原始 GSA JSON 中的文本编码?

Please help me understand what is going on here and how I can avoid this problem.

请帮助我了解这里发生了什么以及如何避免这个问题。

回答by Hari Reddy

The text encoding problem was happening because the call which is made to the GSA server using Apache HTTP Client was using a default content encoding character set of iso-8859-1but the GSA server expected the HTTP Client request and response to be in UTF-8encoding.

发生文本编码问题是因为使用 Apache HTTP 客户端对 GSA 服务器进行的调用使用的是默认内容编码字符集,iso-8859-1但 GSA 服务器希望 HTTP 客户端请求和响应处于UTF-8编码状态。

This problem got resolved after setting the encoding for HTTPClient -

为 HTTPClient 设置编码后,此问题得到解决 -

HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("UTF-8");

And the servlet response encoding to

和 servlet 响应编码到

response.setContentType("application/json;charset=UTF-8");

response.setContentType("application/json;charset=UTF-8");