简体   繁体   English

如何从java中的url编码字符串(不是URL)中获取参数

[英]How to get parameters out of a url encoded string (not a URL) in java

I have a url encoded string that is returned from an API I am using. 我有一个url编码的字符串,从我正在使用的API返回。 I want to do something request.getParameter("paramname") on the string. 我想在字符串上做一些request.getParameter(“paramname”)。 What I'm looking for is something like str.request.getParameter("paramname"). 我正在寻找的是像str.request.getParameter(“paramname”)。 There's gotta be something like this right? 有这样的事情吗?

clarification the api returns something like: name1=val1&name2=val2&name3=val3 澄清api返回的内容如下:name1 = val1&name2 = val2&name3 = val3

I know i could do a split on "&" and then go through each element but that seems stupid. 我知道我可以对“&”进行拆分,然后浏览每个元素,但这看起来很愚蠢。 Please advise. 请指教。 Thanks! 谢谢!

Use URLEncodedUtils from Apache httpclient library. 使用Apache httpclient库中的URLEncodedUtils。

API : http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html API: http//hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

You will have to call this method to get name value pairs: 您必须调用此方法来获取名称值对:

static List<NameValuePair>  parse(HttpEntity entity)
          Returns a list of NameValuePairs as parsed from an HttpEntity.

See this question 看到这个问题

One answer there: 一个答案:

import org.eclipse.jetty.util.*;

MultiMap<String> params = new MultiMap<String>();
UrlEncoded.decodeTo("foo=bar&bla=blub", params, "UTF-8");

assert params.getString("foo").equals("bar");
assert params.getString("bla").equals("blub");

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

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