问题描述
所以我假设我有一个字符串,如
So let's suppose I have a string like
param1 = value1& param2 = {url:http:// somesite .com?someparam = somevalue& someparam1 = somevalue1}& param3 = value3
我需要它:
param1:value1
param2 :{url:http://somesite.com?someparam=somevalue&someparam1=somevalue1}
param3:value3
用Java解析这个问题的最佳方法是什么?到目前为止,我找不到标准Java库的解决方案,我不想重新发明轮子。
What would be the best approach to parse this in Java? So far I could not found a solution with standard Java libraries, and I don't want to reinvent the wheel.
我尝试过(但它不起作用)如果我只放置像我这样的查询参数):
I've tried with (but it would not work if I put there only query parameters like mine):
String url = "http://www.example.com/something.html?one=11111&two=22222&three=33333";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url), "UTF-8");
for (NameValuePair param : params) {
System.out.println(param.getName() + " : " + param.getValue());
}
推荐答案
你为什么不用类似于正则表达式:
Why don't you use something like a regex :
例如像这样一个。* \\?param1 =(。*)& param2 =( 。*)& param3 =(。*)$
这适用于您的网址示例,这就是为什么我添加。* \\?
part;)
for example like this one ".*\\?param1=(.*)¶m2=(.*)¶m3=(.*)$"
this works for your url sample that's why I added the .*\\?
part ;)
这将适用于第一个样本(param1 = value1& param2 = {url:http:/ /somesite.com?someparam=somevalue&someparam1=somevalue1}& param3 = value3
- >
param1 =(。*) & param2 =(。*)& param3 =(。*)$
and this will work for the first sample ("param1=value1¶m2={"url":"http://somesite.com?someparam=somevalue&someparam1=somevalue1"}¶m3=value3"
--> param1=(.*)¶m2=(.*)¶m3=(.*)$
当然,如果您的参数名称不是你不知道
Of course if your params names aren't also something you don't know about
这篇关于从Java中的查询字符串中提取参数及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!