如何从 url 查询中将变量提取为 java 中的可读格式?
How to extract variables from url query to readable format in java?
我有这个url:
http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true
如何以 可读 的方式提取 from
和 to
参数?
我试过以下方法:
String patternString1 = "(&to=) (.+?) (&returnGeometries) (.+?) (&hour=)"
+" (.+?) (&from=) (.+?) (&sameResultType=)";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(freshResponse.regression_requestUrl);
while(matcher.find()) {
System.out.println("found: "+matcher.group(1)+" "+matcher.group(3)+matcher.group(4));
}
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(url);
但即使我成功获取了正确的子字符串,我怎样才能将它们转换为我可以用来找到这个地方的坐标? (换句话说:..这样坐标就干净了,可以使用了)
这应该可以满足您的要求:
public class DecodeURL {
public static void main(String[] args) throws UnsupportedEncodingException {
String input = "http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+"
+"x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3A"
+"true+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds="
+"true&returnInstructions=true &hour=12+00&from=s%3A-1+d%3Afalse+f%3A-"
+"1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn"
+"%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true";
String decoded = java.net.URLDecoder.decode(input, "UTF-8").replace("&", " & ");
String[] params = {"s","d","f","x","y","r","cd","fn","tn","bd","st"};
System.out.println("Decoded input URL: \n"+decoded);
// Output all FROM arguments
System.out.println("\nFROM:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "from", params[i]));
}
// Output all TO arguments
System.out.println("\nTO:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "to", params[i]));
}
}
public static String findInstance(String input, String type, String match) {
int start = input.indexOf(match+":", input.indexOf(type))+match.length()+1;
return input.substring(start, input.indexOf(" ", start));
}
}
输出
Decoded input URL:
http://myhost.com/Request?to=s:73746647 d:false f:-1.0 x:-74.454383 y:40.843021 r:-1.0 cd:-1.0 fn:-1 tn:-1 bd:true st:Campus~Dr & returnGeometries=true & nPaths=1 & returnClientIds=true & returnInstructions=true & hour=12 00 & from=s:-1 d:false f:-1.0 x:-74.241765 y:40.830182 r:-1.0 cd:-1.0 fn:56481485 tn:26459042 bd:false st:Claremont~Ave & sameResultType=true
FROM:
s = -1
d = false
f = -1.0
x = -74.241765
y = 40.830182
r = -1.0
cd = -1.0
fn = 56481485
tn = 26459042
bd = false
st = Claremont~Ave
TO:
s = 73746647
d = false
f = -1.0
x = -74.454383
y = 40.843021
r = -1.0
cd = -1.0
fn = -1
tn = -1
bd = true
st = Campus~Dr
要更改输出参数的数量,只需编辑 params
数组即可。比如你有String[] params = {"x","y"}
,程序只会输出坐标(x,y)
希望对您有所帮助。祝你好运!
试试这个:
String url = "http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true";
URL urlObject = new URL(url);
for (String s : urlObject.getQuery().split("&")) {
String d = URLDecoder.decode(s, "UTF-8");
if (d.startsWith("from=") || d.startsWith("to=")) {
int index = d.indexOf('=') + 1;
System.out.println(d.substring(0, index));
for (String t : d.substring(index).split(" "))
System.out.println(" " + t);
} else
System.out.println(s);
}
URLDecoder.decode()
很有用。但要小心查询字符串可能包含 %26
。它是 &
但不是定界符。
所以你应该 split("&")
然后 decode()
.
我有这个url:
http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true
如何以 可读 的方式提取 from
和 to
参数?
我试过以下方法:
String patternString1 = "(&to=) (.+?) (&returnGeometries) (.+?) (&hour=)"
+" (.+?) (&from=) (.+?) (&sameResultType=)";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(freshResponse.regression_requestUrl);
while(matcher.find()) {
System.out.println("found: "+matcher.group(1)+" "+matcher.group(3)+matcher.group(4));
}
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(url);
但即使我成功获取了正确的子字符串,我怎样才能将它们转换为我可以用来找到这个地方的坐标? (换句话说:..这样坐标就干净了,可以使用了)
这应该可以满足您的要求:
public class DecodeURL {
public static void main(String[] args) throws UnsupportedEncodingException {
String input = "http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+"
+"x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3A"
+"true+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds="
+"true&returnInstructions=true &hour=12+00&from=s%3A-1+d%3Afalse+f%3A-"
+"1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn"
+"%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true";
String decoded = java.net.URLDecoder.decode(input, "UTF-8").replace("&", " & ");
String[] params = {"s","d","f","x","y","r","cd","fn","tn","bd","st"};
System.out.println("Decoded input URL: \n"+decoded);
// Output all FROM arguments
System.out.println("\nFROM:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "from", params[i]));
}
// Output all TO arguments
System.out.println("\nTO:");
for (int i = 0; i < params.length; i++) {
System.out.println(params[i]+" = \t"+findInstance(decoded, "to", params[i]));
}
}
public static String findInstance(String input, String type, String match) {
int start = input.indexOf(match+":", input.indexOf(type))+match.length()+1;
return input.substring(start, input.indexOf(" ", start));
}
}
输出
Decoded input URL:
http://myhost.com/Request?to=s:73746647 d:false f:-1.0 x:-74.454383 y:40.843021 r:-1.0 cd:-1.0 fn:-1 tn:-1 bd:true st:Campus~Dr & returnGeometries=true & nPaths=1 & returnClientIds=true & returnInstructions=true & hour=12 00 & from=s:-1 d:false f:-1.0 x:-74.241765 y:40.830182 r:-1.0 cd:-1.0 fn:56481485 tn:26459042 bd:false st:Claremont~Ave & sameResultType=true
FROM:
s = -1
d = false
f = -1.0
x = -74.241765
y = 40.830182
r = -1.0
cd = -1.0
fn = 56481485
tn = 26459042
bd = false
st = Claremont~Ave
TO:
s = 73746647
d = false
f = -1.0
x = -74.454383
y = 40.843021
r = -1.0
cd = -1.0
fn = -1
tn = -1
bd = true
st = Campus~Dr
要更改输出参数的数量,只需编辑 params
数组即可。比如你有String[] params = {"x","y"}
,程序只会输出坐标(x,y)
希望对您有所帮助。祝你好运!
试试这个:
String url = "http://myhost.com/Request?to=s%3A73746647+d%3Afalse+f%3A-1.0+x%3A-74.454383+y%3A40.843021+r%3A-1.0+cd%3A-1.0+fn%3A-1+tn%3A-1+bd%3Atrue+st%3ACampus%7EDr&returnGeometries=true&nPaths=1&returnClientIds=true&returnInstructions=true&hour=12+00&from=s%3A-1+d%3Afalse+f%3A-1.0+x%3A-74.241765+y%3A40.830182+r%3A-1.0+cd%3A-1.0+fn%3A56481485+tn%3A26459042+bd%3Afalse+st%3AClaremont%7EAve&sameResultType=true";
URL urlObject = new URL(url);
for (String s : urlObject.getQuery().split("&")) {
String d = URLDecoder.decode(s, "UTF-8");
if (d.startsWith("from=") || d.startsWith("to=")) {
int index = d.indexOf('=') + 1;
System.out.println(d.substring(0, index));
for (String t : d.substring(index).split(" "))
System.out.println(" " + t);
} else
System.out.println(s);
}
URLDecoder.decode()
很有用。但要小心查询字符串可能包含 %26
。它是 &
但不是定界符。
所以你应该 split("&")
然后 decode()
.