简体   繁体   English

将字符串转换为JSONObject并返回字符串,而不会丢失UTF-8编码

[英]String to JSONObject and back to String without losing UTF-8 encoding

I've got the following result in String 我在String得到以下result

{
   "html_attributions" : [],
   "result" : {
      "opening_hours" : {
         "open_now" : true,
         "weekday_text" : [
            "Monday: 8:00 AM – 7:00 PM",
            "Tuesday: 8:00 AM – 7:00 PM",
            "Wednesday: 8:00 AM – 2:30 PM",
            "Thursday: 8:00 AM – 7:00 PM",
            "Friday: 8:00 AM – 7:30 PM",
            "Saturday: 8:00 AM – 6:00 PM",
            "Sunday: Closed"
         ]
      }
   },
   "status" : "OK"
}

which I convert to a JSONObject using 我将其转换为JSONObject

JSONObject resultJSON = new JSONObject(result);

I want to get the value of inner weekday_text key but it seems that the final output is not in UTF-8 ( \– unicode characters appear instead): 我想获取内部weekday_text键的值,但最终输出似乎不是UTF-8 (而是显示\– unicode字符):

System.out.println(resultJSON.getJSONObject("result").getJSONObject("opening_hours").getJSONArray("weekday_text").toString());

["Monday: 8:00 AM \u2013 7:00 PM","Tuesday: 8:00 AM \u2013 7:00 PM","Wednesday: 8:00 AM \u2013 2:30 PM","Thursday: 8:00 AM \u2013 7:00 PM","Friday: 8:00 AM \u2013 7:30 PM","Saturday: 8:00 AM \u2013 6:00 PM","Sunday: Closed"]

What am I missing here? 我在这里想念什么?

You are seeing unicode escape sequences because of how toString is implemented. 由于toString的实现方式,您将看到Unicode转义序列。 It is implemented this way probably to make it clear which characters are in the string, which makes it easier to debug your code, because sometimes different code points can look very similar. 可以通过这种方式来实现它,以弄清楚字符串中包含哪些字符,从而使调试代码变得更加容易,因为有时不同的代码点看起来非常相似。

The actual strings are still unescaped. 实际的字符串仍未转义。 Printing individual strings in the array will not show escape sequences: 在数组中打印单个字符串将不会显示转义序列:

System.out.println(resultJSON.getJSONObject("result").getJSONObject("opening_hours").getJSONArray("weekday_text").getString(0));

You can use a OutputStreamWriter to manage the output encoding. 您可以使用OutputStreamWriter来管理输出编码。

JSONObject resultJSON = new JSONObject(result);    
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
resultJSON.write(writer);
writer.flush();

String jSONStringUTF8 = new String(out.toByteArray(),"utf-8");
JSONObject newJSON = new JSONObject(jSONStringUTF8);
String value = newJSON.getString("content");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM