Java中将十六进制unicode字符串转换为普通字符串

问题描述 投票:0回答:2

我正在进行 HTTP 调用,其响应通常类似于(摘录,非完整响应)

"commandMetadata":{"webCommandMetadata":{"url":"/list?v=FA0ORzV4jfw\u0026pp=QAFIAQ%3D%3D","webPageType":"WEB_PAGE_TYPE_WATCH","rootVe":3832}},"watchEndpoint":{"parentId":"FA0ORzV4jfw","params":"EAEYAdoBBAgBKgA%3D","playerParams":"QAFIAQ%3D%3D"

我正在查看响应是否包含特定子字符串,例如。

"parentId":
使用正则表达式。


虽然我的代码在大多数情况下工作正常,但不幸的是,对于某些请求(间歇性),响应似乎采用 unicode 十六进制格式,例如
\x22commandMetadata\x22:\x7b\x22webCommandMetadata\x22:\x7b\x22sendPost\x22:true,\x22apiUrl\x22:\x22\/tmapi\/v1\/like\/like\x22\x7d\x7d,\x22likeEndpoint\x22:\x7b\x22status\x22:\x22LIKE\x22,\x22target\x22:\x7b\x22parentId\x22:\x22O3PGXbXja4g\x22\x7d,\x22likeParams\x22:\x22

因此,我的正则表达式匹配间歇性失败。

我尝试将字符串转换为utf-8,但这似乎不起作用

String decodedResponse = new String(rawResponse.getBytes(), StandardCharsets.UTF_8);

我的代码

if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();

                // Read the response line by line
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();


                String rawResponse = response.toString();
                //LOG.info("rawResponse {}",rawResponse);
                String decodedResponse = new String(rawResponse.getBytes(), StandardCharsets.UTF_8);
                LOG.info("decodedResponse {}",decodedResponse);

                String regex = "\"parentId\":\"([^\"]+)\"";
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(decodedResponse);

如何将十六进制编码的字符串 like 转换为正常的 for like 以便正则表达式起作用?
非常感谢一些帮助。

java java-7 unicode-string unicode-literals
2个回答
0
投票

由于您期待 JSON,并且可以猜测哪些字符可能以“错误”格式出现,所以我建议最简单的解决方案:

public String replaceHex(String s) {
   return s
      .replace("\\x22", "\"")
      .replace("\\x7b", "{")
      .replace("\\x7d", "}");
}

0
投票

您需要一种方法来取消转义响应的字符串...

实现此目的的一种方法是使用 Apache Commons Lib,如下所示:

    String escapedSting = "list?v=FA0ORzV4jfw\\u0026pp=QAFIAQ%3D%3D";
    System.out.println ("escapedSting:"+escapedSting);
    String result = new String (escapedSting.getBytes (), StandardCharsets.UTF_8);
    System.out.println ("result:" + StringEscapeUtils.unescapeJava (result));

结果:

escapedSting:list?v=FA0ORzV4jfw\u0026pp=QAFIAQ%3D%3D 结果:列表?v=FA0ORzV4jfw&pp=QAFIAQ%3D%3D

© www.soinside.com 2019 - 2024. All rights reserved.