我正在发起一个 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":
使用正则表达式。
\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 以便正则表达式起作用?
非常感谢一些帮助。
由于您期待 JSON,并且可以猜测哪些字符可能以错误的格式出现,所以我建议最简单的解决方案:
public String replaceHex(String s) {
return s
.replace("\\x22", "\"")
.replace("\\x7b", "{")
.replace("\\x7d", "}");
}