How to get request parameter as a HashMap

I want my server to be able to pass a GET suh as request:

 http://example.com/?a[foo]=1&a[bar]=15&b=3

And when I get the param 'a' request, it should be parsed as a HashMap like this:

{'foo':1, 'bar':15}

EDIT: Well, to be clear, here is what I am trying to do, but in Java instead of PHP:

Pass array with keys via HTTP GET

Any ideas how to do this?

+5
source share
3 answers

There is no standard way to do this.

Wink supports javax.ws.rs.core.MultivaluedMap. Therefore, if you send something like http://example.com/?a=1&a=15&b=3, you will receive: key avalues ​​1, 15; key bvalue 3.

If you need to parse something like ?a[1]=1&a[gv]=15&b=3, you need to take javax.ws.rs.core.MultivaluedMap.entrySet()and perform additional parsing of the keys.

, ( , ):

String getArrayParameter(String key, String index,  MultivaluedMap<String, String> queryParameters) {
    for (Entry<String, List<String>> entry : queryParameters.entrySet()) {
        String qKey = entry.getKey();
        int a = qKey.indexOf('[');
        if (a < 0) {
            // not an array parameter
            continue;
        }
        int b = qKey.indexOf(']');
        if (b <= a) {
            // not an array parameter
            continue;
        }
        if (qKey.substring(0, a).equals(key)) {
            if (qKey.substring(a + 1, b).equals(index)) {
                return entry.getValue().get(0);
            }
        }
    }
    return null;
}

:

@GET
public void getResource(@Context UriInfo uriInfo) {
    MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
    getArrayParameter("a", "foo", queryParameters);
}
+1

, HttpServletRequest, getParameterMap(), .

.

public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws IOException {

   System.out.println("Parameters : \n"+request.getParameterMap()+"\n End");

}
0

All Articles